home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / cc / g++-dist / cplus-class.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-02-27  |  179.1 KB  |  6,024 lines

  1. /* Functions related to building and playing with classes.
  2.    Copyright (C) 1987 Free Software Foundation, Inc.
  3.    Contributed by Michael Tiemann (tiemann@mcc.com)
  4.  
  5. This file is part of GNU CC.
  6.  
  7. GNU CC is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 1, or (at your option)
  10. any later version.
  11.  
  12. GNU CC is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with GNU CC; see the file COPYING.  If not, write to
  19. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21.  
  22. /* High-level class interface. */
  23.  
  24. #include "config.h"
  25. #include "tree.h"
  26. #include "cplus-tree.h"
  27. #include "flags.h"
  28. #include "rtl.h"
  29. #include "assert.h"
  30. #include <stdio.h>
  31.  
  32. #include "obstack.h"
  33. #define obstack_chunk_alloc xmalloc
  34. #define obstack_chunk_free free
  35.  
  36. extern int xmalloc ();
  37. extern void free ();
  38.  
  39. #define NULL 0
  40. #define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
  41. #define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
  42.  
  43. /* See cplus-decl.c for comment of this variable.  */
  44. extern int flag_int_enum_equivalence;
  45.  
  46. /* some statistics gathering help.  */
  47. static int n_vtables, n_vtable_entries, n_vtable_searches, n_vtable_elems;
  48. static int n_convert_harshness, n_compute_conversion_costs, n_build_method_call;
  49. static int n_inner_fields_searched;
  50.  
  51. /* Compute the ease with which a conversion can be performed
  52.    between an expected and the given type.  */
  53. static int convert_harshness ();
  54.  
  55. /* in decl.c.  */
  56. extern tree lookup_tag_current_binding_level ();
  57.  
  58. /* in method.c.  */
  59. extern void do_inline_function_hair ();
  60.  
  61. /* Way of stacking class types.  */
  62. static tree *current_class_base, *current_class_stack;
  63. static int current_class_stacksize;
  64.  
  65. struct class_level
  66. {
  67.   /* The previous class level.  */
  68.   struct class_level *level_chain;
  69.  
  70.   /* The class instance variable, as a PARM_DECL.  */
  71.   tree decl;
  72.   /* The class instance variable, as an object.  */
  73.   tree object;
  74.   /* The virtual function table pointer
  75.      for the class instance variable.  */
  76.   tree vtable_decl;
  77.  
  78.   /* Name of the current class.  */
  79.   tree name;
  80.   /* Type of the current class.  */
  81.   tree type;
  82.  
  83.   /* Flags for this class level.  */
  84.   int this_is_variable;
  85.   int memoized_lookups;
  86.   int save_memoized;
  87.   int unused;
  88. };
  89.  
  90. tree current_class_decl, C_C_D;    /* PARM_DECL: the class instance variable */
  91. tree current_vtable_decl;
  92.  
  93. /* The following two can be derived from the previous one */
  94. tree current_class_name;    /* IDENTIFIER_NODE: name of current class */
  95. tree current_class_type;    /* _TYPE: the type of the current class */
  96. tree prev_class_type;        /* _TYPE: the previous type that was a class */
  97.  
  98. static tree get_vtable_name (), get_vfield_name ();
  99. tree the_null_vtable_entry;
  100.  
  101. /* Way of stacking langauge names.  */
  102. static tree *current_lang_base, *current_lang_stack;
  103. static int current_lang_stacksize;
  104.  
  105. /* Names of languages we recognize.  */
  106. tree lang_name_c, lang_name_cplusplus;
  107. tree current_lang_name;
  108.  
  109. tree minus_one_node;
  110.  
  111. /* When layout out an aggregate type, the size of the
  112.    basetypes (virtual and non-virtual) is passed to layout_record
  113.    via this node.  */
  114. static tree base_layout_decl;
  115.  
  116. #if 0
  117. /* Make sure that the tag NAME is defined *in the current binding level*
  118.    at least as a forward reference.
  119.    CODE says which kind of tag NAME ought to be.
  120.  
  121.    Not used for C++.  Not maintained.  */
  122.  
  123. tree
  124. start_struct (code, name)
  125.      enum tree_code code;
  126.      tree name;
  127. {
  128.   /* If there is already a tag defined at this binding level
  129.      (as a forward reference), just return it.  */
  130.   register tree ref = 0;
  131.  
  132.   if (name != 0)
  133.     ref = lookup_tag (code, name, current_binding_level, 1);
  134.   if (ref && TREE_CODE (ref) == code)
  135.     {
  136.       if (TYPE_FIELDS (ref))
  137.     error ((code == UNION_TYPE ? "redefinition of `union %s'"
  138.         : "redefinition of `struct %s'"),
  139.            IDENTIFIER_POINTER (name));
  140.  
  141.       return ref;
  142.     }
  143.  
  144.   /* Otherwise create a forward-reference just so the tag is in scope.  */
  145.  
  146.   ref = make_lang_type (code);
  147.   /* Must re-synch this with xref_tag if you are going to use it.  */
  148.   assert (0);
  149.   pushtag (name, ref);
  150.   return ref;
  151. }
  152. #endif
  153.  
  154. /* Virtual baseclass things.  */
  155. tree
  156. build_vbase_pointer (exp, type)
  157.      tree exp, type;
  158. {
  159.   char *name;
  160.  
  161.   name = (char *) alloca (TYPE_NAME_LENGTH (type) + sizeof (VBASE_NAME) + 1);
  162.   sprintf (name, VBASE_NAME_FORMAT, TYPE_NAME_STRING (type));
  163.   return build_component_ref (exp, get_identifier (name), 0, 0);
  164. }
  165.  
  166. /* Build multi-level access to EXPR using hierarchy path PATH.
  167.    CODE is PLUS_EXPR if we are going with the grain,
  168.    and MINUS_EXPR if we are not (in which case, we cannot traverse
  169.    virtual baseclass links).
  170.  
  171.    TYPE is the type we want this path to have on exit.
  172.  
  173.    ALIAS_THIS is non-zero if EXPR in an expression involving `this'.  */
  174. tree
  175. build_vbase_path (code, type, expr, path, alias_this)
  176.      enum tree_code code;
  177.      tree type;
  178.      tree expr;
  179.      tree path;
  180.      int alias_this;
  181. {
  182.   register int changed = 0;
  183.   tree last = NULL_TREE, last_virtual = NULL_TREE;
  184.   int fixed_type_p = 0 && resolves_to_fixed_type_p (expr);
  185.   tree basetype;
  186.   tree offset = integer_zero_node;
  187.  
  188.   if (TREE_CHAIN (path))
  189.     path = nreverse (copy_list (path));
  190.  
  191.   basetype = TREE_VALUE (path);
  192.   while (path)
  193.     {
  194.       if (TREE_VIA_VIRTUAL (path))
  195.     {
  196.       last_virtual = TYPE_MAIN_VARIANT (TREE_VALUE (path));
  197.       if (code == PLUS_EXPR)
  198.         {
  199.           changed = ! fixed_type_p;
  200.  
  201.           if (last)
  202.         expr = convert_pointer_to (TREE_VALUE (last), expr);
  203.           if (changed)
  204.         expr = build_vbase_pointer (build_indirect_ref (expr, 0),
  205.                         last_virtual);
  206.           else
  207.         offset = ASSOC_OFFSET (value_member (last_virtual,
  208.                              CLASSTYPE_VBASECLASSES (basetype)));
  209.         }
  210.       else
  211.         {
  212.           error_with_aggr_type (last_virtual, "cannot cast up from virtual baseclass `%s'");
  213.           return error_mark_node;
  214.         }
  215.     }
  216.       last = path;
  217.       path = TREE_CHAIN (path);
  218.     }
  219.   /* LAST is now the last basetype on the path.  */
  220.   last = TREE_VALUE (last);
  221.  
  222.   /* If we go through any virtual base pointers, make sure that
  223.      casts to BASETYPE from the last virtual base class use
  224.      the right value for BASETYPE.  */
  225.   if (changed)
  226.     {
  227.       tree intype = TREE_TYPE (TREE_TYPE (expr));
  228.       if (TYPE_MAIN_VARIANT (intype) == TYPE_MAIN_VARIANT (last))
  229.     basetype = intype;
  230.       else
  231.     {
  232.       basetype = get_base_type (last, TYPE_MAIN_VARIANT (intype), 0);
  233.       offset = CLASSTYPE_OFFSET (basetype);
  234.     }
  235.     }
  236.   else
  237.     {
  238.       if (last_virtual && last != last_virtual)
  239.     basetype = get_base_type (last, last_virtual, 0);
  240.       else
  241.     basetype = last;
  242.  
  243.       offset = genop (code, offset, CLASSTYPE_OFFSET (basetype));
  244.     }
  245.  
  246.   if (TREE_INT_CST_LOW (offset))
  247.     {
  248.       /* For multiple inheritance: if `this' can be set by
  249.      any function, then it could be 0 on entry
  250.      to any function.  Preserve such zeroness here.
  251.      Otherwise, only in the case of constructors need
  252.      we worry, and in those cases, it will be zero,
  253.      or initialized to some legal value to which we may
  254.      add.  */
  255.       tree addr = TREE_CODE (expr) == ADDR_EXPR
  256.     ? expr : save_expr (expr);
  257.       expr = build (code, type, addr, offset);
  258.  
  259.       if (alias_this == 0 || flag_this_is_variable)
  260.     return build (COND_EXPR, type,
  261.               build (EQ_EXPR, integer_type_node, addr, integer_zero_node),
  262.               build1 (NOP_EXPR, type, addr),
  263.               expr);
  264.     }
  265.   return build1 (NOP_EXPR, type, expr);
  266. }
  267.  
  268. /* Virtual function things.  */
  269.  
  270. /* Virtual functions to be dealt with after laying out our
  271.    virtual base classes (only if the type has any).  */
  272. static tree pending_hard_virtuals;
  273.  
  274. /* The names of the entries in the virtual table structure.  */
  275. static tree delta_name, pfn_name;
  276.  
  277. /* Temporary assoc list to memoize lookups of the left-most non-virtual
  278.    baseclass B in a lattice topped by T.  B can appear multiple times
  279.    in the lattice.
  280.    TREE_PURPOSE is B's TYPE_MAIN_VARIANT.
  281.    TREE_VALUE is the path by which B is reached from T.
  282.    TREE_TYPE is B's real type.
  283.  
  284.    If TREE_TYPE is NULL_TREE, it means that B was reached via
  285.    a virtual baseclass.
  286.    N.B.: This list consists of nodes on the temporary obstack.  */
  287. static tree leftmost_baseclasses;
  288.  
  289. /* Build an entry in the virtual function table.
  290.    DELTA is the offset for the `this' pointer.
  291.    PFN is an ADDR_EXPR containing a pointer to the virtual function.
  292.    Note that the index (DELTA2) in the virtual function table
  293.    is always 0.  */
  294. tree
  295. build_vtable_entry (delta, pfn)
  296.      tree delta, pfn;
  297. {
  298.   tree elems = tree_cons (NULL_TREE, delta,
  299.               tree_cons (NULL_TREE, integer_zero_node,
  300.                      build_tree_list (NULL_TREE, pfn)));
  301.   tree entry = build (CONSTRUCTOR, vtable_entry_type, NULL_TREE, elems);
  302.   TREE_LITERAL (entry) = 1;
  303.   TREE_STATIC (entry) = 1;
  304.   TREE_READONLY (entry) = 1;
  305.  
  306. #ifdef GATHER_STATISTICS
  307.   n_vtable_entries += 1;
  308. #endif
  309.  
  310.   return entry;
  311. }
  312.  
  313. /* Given an object INSTANCE, return an expression which yields
  314.    the virtual function corresponding to INDEX.  There are many special
  315.    cases for INSTANCE which we take care of here, mainly to avoid
  316.    creating extra tree nodes when we don't have to.  */
  317. tree
  318. build_vfn_ref (ptr_to_instptr, instance, index)
  319.      tree *ptr_to_instptr, instance;
  320.      tree index;
  321. {
  322.   extern int building_cleanup;
  323.   tree vtbl, aref;
  324.   tree basetype = TREE_TYPE (instance);
  325.  
  326.   if (TREE_CODE (basetype) == REFERENCE_TYPE)
  327.     basetype = TREE_TYPE (basetype);
  328.  
  329.   if (instance == C_C_D)
  330.     {
  331.       if (current_vtable_decl == NULL_TREE
  332.       || current_vtable_decl == error_mark_node
  333.       || get_base_type (DECL_FCONTEXT (CLASSTYPE_VFIELD (current_class_type)), basetype, 0) == NULL_TREE)
  334.     vtbl = build_indirect_ref (build_vfield_ref (instance, basetype));
  335.       else
  336.     vtbl = current_vtable_decl;
  337.     }
  338.   else
  339.     {
  340.       if (optimize)
  341.     {
  342.       /* Try to figure out what a reference refers to, and
  343.          access its virtual function table directly.  */
  344.       tree ref = 0;
  345.  
  346.       if (TREE_CODE (instance) == INDIRECT_REF
  347.           && TREE_CODE (TREE_TYPE (TREE_OPERAND (instance, 0))) == REFERENCE_TYPE)
  348.         ref = TREE_OPERAND (instance, 0);
  349.       else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
  350.         ref = instance;
  351.  
  352.       if (ref && TREE_CODE (ref) == VAR_DECL
  353.           && DECL_INITIAL (ref))
  354.         {
  355.           tree init = DECL_INITIAL (ref);
  356.  
  357.           while (TREE_CODE (init) == NOP_EXPR
  358.              || TREE_CODE (init) == REFERENCE_EXPR)
  359.         init = TREE_OPERAND (init, 0);
  360.           if (TREE_CODE (init) == ADDR_EXPR)
  361.         {
  362.           init = TREE_OPERAND (init, 0);
  363.           if (IS_AGGR_TYPE (TREE_TYPE (init))
  364.               && (TREE_CODE (init) == PARM_DECL
  365.               || TREE_CODE (init) == VAR_DECL))
  366.             instance = init;
  367.         }
  368.         }
  369.     }
  370.  
  371.       if (IS_AGGR_TYPE (instance)
  372.       && (TREE_CODE (instance) == RESULT_DECL
  373.           || TREE_CODE (instance) == PARM_DECL
  374.           || TREE_CODE (instance) == VAR_DECL))
  375.     vtbl = CLASS_ASSOC_VTABLE (basetype);
  376.       else
  377.     vtbl = build_indirect_ref (build_vfield_ref (instance, basetype), 0);
  378.     }
  379.   aref = build_array_ref (vtbl, index);
  380.   if (!building_cleanup && TREE_CODE (aref) == INDIRECT_REF)
  381.     TREE_OPERAND (aref, 0) = save_expr (TREE_OPERAND (aref, 0));
  382.  
  383.   *ptr_to_instptr = build (PLUS_EXPR, TREE_TYPE (*ptr_to_instptr),
  384.                *ptr_to_instptr,
  385.                convert (integer_type_node, build_component_ref (aref, delta_name, 0, 0)));
  386.   return build_component_ref (aref, pfn_name, 0, 0);
  387. }
  388.  
  389. /* Build a virtual function for type TYPE.
  390.    If ASSOC is non-NULL, build the vtable starting with the intial
  391.    approximation that it is the same as the one which is the head of
  392.    the assocation list.  */
  393. static tree
  394. build_vtable (assoc, type)
  395.      tree assoc, type;
  396. {
  397.   tree name = get_vtable_name (type);
  398.   tree virtuals, decl;
  399.  
  400.   if (assoc)
  401.     {
  402.       virtuals = copy_list (ASSOC_VIRTUALS (assoc));
  403.       decl = build_decl (VAR_DECL, name, TREE_TYPE (ASSOC_VTABLE (assoc)));
  404.     }
  405.   else
  406.     {
  407.       virtuals = NULL_TREE;
  408.       decl = build_decl (VAR_DECL, name, void_type_node);
  409.     }
  410.  
  411. #ifdef GATHER_STATISTICS
  412.   n_vtables += 1;
  413.   n_vtable_elems += list_length (virtuals);
  414. #endif
  415.  
  416.   if (write_virtuals > 0)
  417.     TREE_PUBLIC (decl) = 1;
  418.   else if (write_virtuals < 0)
  419.     TREE_EXTERNAL (decl) = 1;
  420.   IDENTIFIER_GLOBAL_VALUE (name) = decl = pushdecl_top_level (decl);
  421.   /* Initialize the association list for this type, based
  422.      on our first approximation.  */
  423.   CLASS_ASSOC_VTABLE (type) = decl;
  424.   CLASS_ASSOC_VIRTUALS (type) = virtuals;
  425.  
  426.   TREE_STATIC (decl) = 1;
  427.   DECL_ALIGN (decl) = MAX (TYPE_ALIGN (double_type_node),
  428.                DECL_ALIGN (decl));
  429.  
  430.   if (assoc && write_virtuals >= 0)
  431.     DECL_VIRTUAL_P (decl) = 1;
  432.   /* Remember which class this vtable is really for.  */
  433.   DECL_VPARENT (decl) = type;
  434.   CLASSTYPE_MARKED3 (type) = 1;
  435.   CLASSTYPE_MARKED4 (type) = 1;
  436.   return decl;
  437. }
  438.  
  439. /* Give TYPE a new virtual function table which is initialized
  440.    with a skeleton-copy of its original initialization.  The only
  441.    entry that changes is the `delta' entry, so we can really
  442.    share a lot of structure.
  443.  
  444.    FOR_TYPE is the derived type which caused this table to
  445.    be needed.
  446.  
  447.    ASSOC is the type association which provided TYPE for FOR_TYPE.
  448.  
  449.    The way we update BASE_ASSOC's vtable information is just to change the
  450.    association information in FOR_TYPE's association list.  */
  451. static void
  452. prepare_fresh_vtable (assoc, base_assoc, for_type)
  453.      tree assoc, base_assoc, for_type;
  454. {
  455.   tree basetype = ASSOC_TYPE (assoc);
  456.   tree orig_decl = ASSOC_VTABLE (assoc);
  457.   tree name = build_type_pathname (VTABLE_NAME_FORMAT, basetype, for_type);
  458.   tree new_decl = build_decl (VAR_DECL, name, TREE_TYPE (orig_decl));
  459.   tree path;
  460.   int result;
  461.  
  462.   assert (TREE_USED (assoc) == 0);
  463.  
  464.   /* Remember which class this vtable is really for.  */
  465.   DECL_VPARENT (new_decl) = ASSOC_TYPE (base_assoc);
  466.  
  467.   /* `new_decl' has the right PUBLIC and EXTERNAL bits set.  */
  468.   TREE_STATIC (new_decl) = 1;
  469.   ASSOC_VTABLE (assoc) = pushdecl_top_level (new_decl);
  470.   DECL_VIRTUAL_P (new_decl) = 1;
  471.   DECL_ALIGN (new_decl) = DECL_ALIGN (orig_decl);
  472.  
  473.   /* Make fresh virtual list, so we can smash it later.  */
  474.   assert (ASSOC_VIRTUALS (assoc));
  475.   ASSOC_VIRTUALS (assoc) = copy_list (ASSOC_VIRTUALS (assoc));
  476.  
  477. #ifdef GATHER_STATISTICS
  478.   n_vtables += 1;
  479.   n_vtable_elems += list_length (ASSOC_VIRTUALS (assoc));
  480. #endif
  481.  
  482.   CLASSTYPE_MARKED3 (basetype) = 1;
  483.   CLASSTYPE_MARKED4 (basetype) = 1;
  484.  
  485.   /* Mark all types between FOR_TYPE and TYPE as having been
  486.      touched, so that if we change virtual function table entries,
  487.      new vtables will be initialized.  We may reach the virtual
  488.      baseclass via ambiguous intervening baseclasses.  This
  489.      loop makes sure we get through to the actual baseclass we marked.  */
  490.  
  491.   do
  492.     {
  493.       result = get_base_distance (basetype, for_type, 0, &path);
  494.       for_type = TREE_VALUE (path);
  495.       while (path)
  496.     {
  497.       CLASSTYPE_MARKED3 (TREE_VALUE (path)) = 1;
  498.       path = TREE_CHAIN (path);
  499.     }
  500.     }
  501.   while (result == -2);
  502. }
  503.  
  504. /* Access the virtual function table entry that logically
  505.    contains BASE_FNDECL.  VIRTUALS is the virtual function table's
  506.    initializer.  */
  507. static tree
  508. get_vtable_entry (virtuals, base_fndecl)
  509.      tree virtuals, base_fndecl;
  510. {
  511.   int i = (HOST_BITS_PER_INT >= BITS_PER_WORD
  512. #ifdef VTABLE_USES_MASK
  513.        && 0
  514. #endif
  515.        ? (TREE_INT_CST_LOW (DECL_VINDEX (base_fndecl))
  516.           & ((1<<(BITS_PER_WORD-1))-1))
  517.        : TREE_INT_CST_LOW (DECL_VINDEX (base_fndecl)));
  518.  
  519. #ifdef GATHER_STATISTICS
  520.   n_vtable_searches += i;
  521. #endif
  522.  
  523.   while (i > 0)
  524.     {
  525.       virtuals = TREE_CHAIN (virtuals);
  526.       i -= 1;
  527.     }
  528.   return virtuals;
  529. }
  530.  
  531. /* Put new entry ENTRY into virtual function table initializer
  532.    VIRTUALS.  The virtual function table is for type CONTEXT.
  533.  
  534.    Also update DECL_VINDEX (FNDECL).  */
  535.  
  536. static void
  537. modify_vtable_entry (old_entry_in_list, new_entry, fndecl, context)
  538.      tree old_entry_in_list, new_entry, fndecl, context;
  539. {
  540.   tree base_pfn = FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (old_entry_in_list));
  541.   tree vindex;
  542.  
  543.   /* We can't put in the really right offset information
  544.      here, since we have not yet laid out the class to
  545.      take into account virtual base classes.  */
  546.   TREE_VALUE (old_entry_in_list) = new_entry;
  547.   vindex = DECL_VINDEX (TREE_OPERAND (base_pfn, 0));
  548.   if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
  549.     SET_DECL_VINDEX (fndecl, vindex);
  550.   else
  551.     {
  552.       if (! tree_int_cst_equal (DECL_VINDEX (fndecl), vindex))
  553.     {
  554.       tree elts = CONSTRUCTOR_ELTS (new_entry);
  555.       tree vfield = CLASSTYPE_VFIELD (context);
  556.       /* Compute the relative offset of vtable we are really looking for.  */
  557.       TREE_VALUE (elts) = genop (PLUS_EXPR,
  558.                      build_int (DECL_OFFSET (vfield)
  559.                         / DECL_SIZE_UNIT (vfield)),
  560.                      TREE_VALUE (elts));
  561.       /* Say what index to use when we use that vtable.  */
  562. #ifndef VTABLE_USES_MASK
  563.       vindex = build_int_2 (TREE_INT_CST_LOW (vindex) & ~(1 << (BITS_PER_WORD -1)), 0);
  564. #endif
  565.       TREE_VALUE (TREE_CHAIN (elts)) = vindex;
  566.     }
  567.     }
  568. }
  569.  
  570. /* Modify virtual function tables in lattice topped by T to
  571.    place FNDECL in tables which previously held BASE_FNDECL.
  572.    PFN is just FNDECL wrapped in an ADDR_EXPR, so that it
  573.    is suitable for placement directly into an initializer.
  574.  
  575.    All distinct virtual function tables that this type uses
  576.    must be updated.  */
  577. static void
  578. modify_vtable_entries (t, fndecl, base_fndecl, pfn)
  579.      tree t;
  580.      tree fndecl, base_fndecl, pfn;
  581. {
  582.   tree base_offset, offset;
  583.   tree context = DECL_CONTEXT (base_fndecl);
  584.   tree vfield = CLASSTYPE_VFIELD (t);
  585.   tree vslots, vbases;
  586.  
  587.   DECL_VCONTEXT (fndecl) = DECL_VCONTEXT (base_fndecl);
  588.  
  589.   if (DECL_CONTEXT (fndecl) == t
  590.       || !(TYPE_USES_VIRTUAL_BASECLASSES (t)
  591.        || TYPE_USES_MULTIPLE_INHERITANCE (t)))
  592.     offset = integer_zero_node;
  593.   else
  594.     {
  595.       tree assoc = value_member (DECL_CONTEXT (fndecl), CLASSTYPE_VBASECLASSES (t));
  596.       if (assoc == NULL_TREE)
  597.     assoc = assoc_value (DECL_CONTEXT (fndecl), t);
  598.       assert (assoc != NULL_TREE);
  599.       offset = ASSOC_OFFSET (assoc);
  600.     }
  601.  
  602.   /* For each layer of base class (i.e., the first base class, and each
  603.      virtual base class from that one), modify the virtual function table
  604.      of the derived class to contain the new virtual function.
  605.      A class has as many vfields as it has virtual base classes (total).  */
  606.   for (vslots = CLASSTYPE_VFIELDS (t); vslots; vslots = TREE_CHAIN (vslots))
  607.     {
  608.       int normal = 1;
  609.       tree assoc, this_offset;
  610.       tree base, path;
  611.  
  612.       /* Find the right base class for this derived class, call it BASE.  */
  613.       base = TREE_VALUE (vslots);
  614.  
  615.       if (base != context)
  616.     {
  617.       /* If BASE_FNDECL is not contained in the vtable accessed by
  618.          the vslot, don't try to modify the vtable.
  619.          
  620.          Virtual functions from virtual baseclasses are not in derived
  621.          virtual function tables.  This is an implementation decision;
  622.          it keeps there from being a combinatorial exposion in the
  623.          number of different vtables which must be maintained.  */
  624.  
  625.       if (get_base_distance (base, context, 0, &path) == -1)
  626.         continue;
  627.  
  628.       /* BASE_FNDECL is defined in a class derived from
  629.          the base class owning this VFIELD.  */
  630.     }
  631.       /* Get the path starting from the deepest base class CONTEXT
  632.      of T (i.e., first defn of BASE_FNDECL).  */
  633.       get_base_distance (context, t, 0, &path);
  634.  
  635.       /* Get our best approximation of what to use for constructing
  636.      the virtual function table for T.  */
  637.       do
  638.     {
  639.       /* Walk from base toward derived, stopping at the
  640.          most derived baseclass that matters.  */
  641.       if (TREE_VIA_VIRTUAL (path))
  642.         {
  643.           base = TREE_VALUE (path);
  644.           assoc = value_member (TYPE_MAIN_VARIANT (base), CLASSTYPE_VBASECLASSES (t));
  645.           break;
  646.         }
  647.       if (TREE_CHAIN (path) == NULL_TREE
  648.           || (CLASSTYPE_BASECLASS (TREE_VALUE (TREE_CHAIN (path)), 1)
  649.           != TREE_VALUE (path))
  650.           || TREE_CHAIN (TREE_CHAIN (path)) == NULL_TREE)
  651.         {
  652.           base = TREE_VALUE (path);
  653.           assoc = assoc_value (TYPE_MAIN_VARIANT (base), t);
  654.           break;
  655.         }
  656.       path = TREE_CHAIN (path);
  657.     }
  658.       while (1);
  659.  
  660.       /* Find the right offset for the this pointer based on the base
  661.      class we just found.  */
  662.       base_offset = ASSOC_OFFSET (assoc);
  663.       if (base_offset == integer_zero_node)
  664.     this_offset = offset;
  665.       else
  666.     this_offset = genop (MINUS_EXPR, offset, base_offset);
  667.  
  668.       /* Make sure we can modify the derived association with immunity.  */
  669.       if (TREE_USED (CLASSTYPE_ASSOC (t)))
  670.     CLASSTYPE_ASSOC (t) = copy_assoc (CLASSTYPE_ASSOC (t));
  671.  
  672.       /* We call this case NORMAL iff this virtual function table
  673.      pointer field has its storage reserved in this class.
  674.      This is normally the case without virtual baseclasses
  675.      or off-center multiple baseclasses.  */
  676.       normal = (vfield != NULL_TREE
  677.         && TREE_VALUE (vslots) == DECL_FCONTEXT (vfield)
  678.         && (TREE_PURPOSE (vslots) == NULL_TREE
  679.             || ! TREE_VIA_VIRTUAL (TREE_PURPOSE (vslots))));
  680.  
  681.       if (normal && TREE_PURPOSE (vslots))
  682.     /* Everything looks normal so far...check that we are really
  683.        working from VFIELD's basetype, and not some other appearance
  684.        of that basetype in the lattice.  */
  685.     normal = (TREE_PURPOSE (vslots) == get_base_type (TREE_VALUE (vslots), t, 0));
  686.  
  687.       if (normal)
  688.     {
  689.       /* In this case, it is *type*'s vtable we are modifying.  */
  690.       context = t;
  691.       if (! CLASSTYPE_MARKED4 (t))
  692.         build_vtable (assoc, t);
  693.       assoc = CLASSTYPE_ASSOC (t);
  694.     }
  695.       else
  696.     {
  697.       /* This is our very own copy of `basetype' to play with.  */
  698.       if (! CLASSTYPE_MARKED4 (ASSOC_TYPE (assoc)))
  699.         prepare_fresh_vtable (assoc, CLASSTYPE_ASSOC (base), t);
  700.     }
  701.  
  702.       modify_vtable_entry (get_vtable_entry (ASSOC_VIRTUALS (assoc), base_fndecl),
  703.                build_vtable_entry (this_offset, pfn),
  704.                fndecl, context);
  705.     }
  706.   for (vbases = CLASSTYPE_VBASECLASSES (t); vbases; vbases = TREE_CHAIN (vbases))
  707.     {
  708.       tree this_offset;
  709.       tree base, path;
  710.  
  711.       if (! ASSOC_VTABLE (vbases))
  712.     /* There are only two ways that a type can fail to have
  713.        virtual functions: neither it nor any of its base
  714.        types define virtual functions (in which case
  715.        no updating need be done), or virtual functions
  716.        accessible to it come from virtual base classes
  717.        (in which case we have or will get them modified
  718.        in other passes of this loop).  */
  719.     continue;
  720.  
  721.       base = TREE_VALUE (vbases);
  722.       path = NULL_TREE;
  723.  
  724.       if (base != context
  725.       && get_base_distance (context, base, 0, &path) == -1)
  726.     continue;
  727.  
  728.       /* Doesn't matter if not actually from this virtual base class,
  729.          but shouldn't come from deeper virtual baseclasses.  The enclosing
  730.      loop should take care of such baseclasses.  */
  731.       while (path)
  732.     {
  733.       if (TREE_VIA_VIRTUAL (path))
  734.         goto skip;
  735.       path = TREE_CHAIN (path);
  736.     }
  737.  
  738.       base_offset = ASSOC_OFFSET (vbases);
  739.       if (base_offset == integer_zero_node)
  740.     this_offset = offset;
  741.       else
  742.     this_offset = genop (MINUS_EXPR, offset, base_offset);
  743.  
  744.       /* Make sure we can modify the derived association with immunity.  */
  745.       if (TREE_USED (CLASSTYPE_ASSOC (t)))
  746.     CLASSTYPE_ASSOC (t) = copy_assoc (CLASSTYPE_ASSOC (t));
  747.  
  748.       /* This is our very own copy of `basetype' to play with.  */
  749.       if (! CLASSTYPE_MARKED4 (ASSOC_TYPE (vbases)))
  750.     {
  751.       tree context_assoc = assoc_value (context, base);
  752.       prepare_fresh_vtable (vbases, context_assoc, t);
  753.     }
  754.       modify_vtable_entry (get_vtable_entry (ASSOC_VIRTUALS (vbases), base_fndecl),
  755.                build_vtable_entry (this_offset, pfn),
  756.                fndecl, context);
  757.     skip: {}
  758.     }
  759. }
  760.  
  761. static tree
  762. add_virtual_function (pending_virtuals, has_virtual, x, first)
  763.      tree pending_virtuals;
  764.      int *has_virtual;
  765.      tree x;
  766.      int first;
  767. {
  768.   int debug_vbase = 1;
  769.  
  770.   /* FUNCTION_TYPEs and OFFSET_TYPEs no longer freely
  771.      convert to void *.  Make such a conversion here.  */
  772.   tree vfn = build1 (ADDR_EXPR, ptr_type_node, x);
  773.   TREE_LITERAL (vfn) = 1;
  774.   TREE_ADDRESSABLE (x) = CLASSTYPE_VTABLE_NEEDS_WRITING (current_class_type);
  775.  
  776.   /* If the virtual function is a redefinition of a prior one,
  777.      figure out in which base class the new definition goes,
  778.      and if necessary, make a fresh virtual function table
  779.      to hold that entry.  */
  780.   if (DECL_VINDEX (x) == NULL_TREE)
  781.     {
  782.       tree entry = build_vtable_entry (integer_zero_node, vfn);
  783.  
  784.       /* Build a new INT_CST for this DECL_VINDEX.  */
  785. #ifdef VTABLE_USES_MASK
  786.       SET_DECL_VINDEX (x, build_int_2 (++(*has_virtual), 0));
  787. #else
  788.       SET_DECL_VINDEX (x, build_int_2 (((1 << (BITS_PER_WORD - 1)) | ++(*has_virtual)), ~0));
  789. #endif
  790.       pending_virtuals = tree_cons (DECL_VINDEX (x), entry, pending_virtuals);
  791.     }
  792.   /* Happens if declared twice in class.  We will give error
  793.      later.  */
  794.   else if (TREE_CODE (DECL_VINDEX (x)) == INTEGER_CST)
  795.     return pending_virtuals;
  796.   else if (debug_vbase && TYPE_USES_VIRTUAL_BASECLASSES (current_class_type))
  797.     {
  798.       /* Need an entry in some other virtual function table.
  799.          Deal with this after we have laid out our virtual base classes.  */
  800.       pending_hard_virtuals = temp_tree_cons (x, vfn, pending_hard_virtuals);
  801.     }
  802.   else
  803.     {
  804.       /* Need an entry in some other virtual function table.
  805.          We can do this now.  */
  806.       tree base_fndecls = DECL_VINDEX (x);
  807.       while (base_fndecls)
  808.     {
  809.       modify_vtable_entries (current_class_type, x,
  810.                  TREE_VALUE (base_fndecls), vfn);
  811.       base_fndecls = TREE_CHAIN (base_fndecls);
  812.     }
  813.     }
  814.   return pending_virtuals;
  815. }
  816.  
  817. /* Obstack on which to build the vector of class methods.  */
  818. struct obstack class_obstack;
  819. extern struct obstack *current_obstack;
  820.  
  821. /* Add method METHOD to class TYPE.  This is used when a method
  822.    has been defined which did not initially appear in the class definition,
  823.    and helps cut down on spurious error messages.
  824.  
  825.    FIELDS is the entry in the METHOD_VEC vector entry of the class type where
  826.    the method should be added.  */
  827. void
  828. add_method (type, fields, method)
  829.      tree type, *fields, method;
  830. {
  831.   /* We must make a copy of METHOD here, since we must be sure that
  832.      we have exclusive title to this method's TREE_CHAIN.  */
  833.   int temp = allocation_temporary_p ();
  834.   tree decl;
  835.  
  836.   if (temp)
  837.     end_temporary_allocation ();
  838.   {
  839.     decl = copy_node (method);
  840.     if (DECL_RTL (decl) == 0)
  841.       make_function_rtl (decl);
  842.   }
  843.  
  844.   if (fields && *fields)
  845.     {
  846.       /* Take care not to hide destructor.  */
  847.       TREE_CHAIN (decl) = TREE_CHAIN (*fields);
  848.       TREE_CHAIN (*fields) = decl;
  849.     }
  850.   else if (CLASSTYPE_METHOD_VEC (type) == 0)
  851.     {
  852.       tree method_vec = make_node (TREE_VEC);
  853.       if (DECL_NAME (TYPE_NAME (type)) == DECL_ORIGINAL_NAME (decl))
  854.     {
  855.       TREE_VEC_ELT (method_vec, 0) = decl;
  856.       TREE_VEC_LENGTH (method_vec) = 1;
  857.     }
  858.       else
  859.     {
  860.       obstack_free (current_obstack, method_vec);
  861.       obstack_blank (current_obstack, sizeof (struct tree_vec) + sizeof (tree *));
  862.       TREE_VEC_ELT (method_vec, 1) = decl;
  863.       TREE_VEC_LENGTH (method_vec) = 2;
  864.       obstack_finish (current_obstack);
  865.     }
  866.       CLASSTYPE_METHOD_VEC (type) = method_vec;
  867.     }
  868.   else
  869.     {
  870.       tree method_vec = CLASSTYPE_METHOD_VEC (type);
  871.       int len = TREE_VEC_LENGTH (method_vec);
  872.  
  873.       /* Adding a new ctor or dtor.  */
  874.       if (DECL_NAME (TYPE_NAME (type)) == DECL_ORIGINAL_NAME (decl))
  875.     TREE_VEC_ELT (method_vec, 0) = decl;
  876.       else
  877.     {
  878.       tree *end = (tree *)obstack_next_free (&class_obstack);
  879.       if (end != TREE_VEC_END (method_vec))
  880.         {
  881.           tree tmp_vec = copy_node (method_vec);
  882.           obstack_copy (current_obstack, &TREE_VEC_ELT (method_vec, 1), len);
  883.           obstack_blank (current_obstack, sizeof (tree *));
  884.           obstack_finish (current_obstack);
  885.           method_vec = tmp_vec;
  886.         }
  887.       else
  888.         {
  889.           /* We can easily extend the last such method_vec created.  */
  890.           obstack_free (&class_obstack, method_vec);
  891.           obstack_blank (&class_obstack,
  892.                  ((char *)end - (char *)method_vec) + sizeof (tree *));
  893.           method_vec = (tree)obstack_base (&class_obstack);
  894.           obstack_finish (&class_obstack);
  895.         }
  896.       TREE_VEC_ELT (method_vec, len) = decl;
  897.       TREE_VEC_LENGTH (method_vec) = len + 1;
  898.       CLASSTYPE_METHOD_VEC (type) = method_vec;
  899.  
  900.       if (CLASSTYPE_BASELINK_VEC (type))
  901.         {
  902.           /* ??? May be better to know whether these can be extended?  */
  903.           tree baselink_vec = copy_node (CLASSTYPE_BASELINK_VEC (type));
  904.  
  905.           obstack_copy (current_obstack, &TREE_VEC_ELT (CLASSTYPE_BASELINK_VEC (type), 1), len);
  906.           TREE_VEC_ELT (baselink_vec, len) = 0;
  907.           TREE_VEC_LENGTH (baselink_vec) = len + 1;
  908.           CLASSTYPE_BASELINK_VEC (type) = baselink_vec;
  909.         }
  910.     }
  911.     }
  912.   DECL_CONTEXT (decl) = type;
  913.   DECL_VCONTEXT (decl) = type;
  914.  
  915.   if (temp)
  916.     resume_temporary_allocation ();
  917. }
  918.  
  919. /* Subroutines of finish_struct.  */
  920.  
  921. /* Look through the list of fields for this struct, deleting
  922.    duplicates as we go.  This must be recursive to handle
  923.    anonymous unions.
  924.  
  925.    FIELD is the field which may not appear anywhere in FIELDS.
  926.    FIELD_PTR, if non-null, is the starting point at which
  927.    chained deletions may take place.
  928.    The value returned is the first acceptable entry found
  929.    in FIELDS.
  930.  
  931.    Note that anonymous fields which are not of UNION_TYPE are
  932.    not duplicates, they are just anonymous fields.  This happens
  933.    when we have unnamed bitfields, for example.  */
  934. static tree
  935. delete_duplicate_fields_1 (field, field_ptr, fields)
  936.      tree field, *field_ptr, fields;
  937. {
  938.   tree x;
  939.   tree prev = field_ptr ? *field_ptr : 0;
  940.   if (DECL_NAME (field) == 0)
  941.     {
  942.       if (TREE_CODE (TREE_TYPE (field)) != UNION_TYPE)
  943.     return fields;
  944.  
  945.       for (x = TYPE_FIELDS (TREE_TYPE (field)); x; x = TREE_CHAIN (x))
  946.     fields = delete_duplicate_fields_1 (x, field_ptr, fields);
  947.       if (prev)
  948.     TREE_CHAIN (prev) = fields;
  949.       return fields;
  950.     }
  951.   else
  952.     {
  953.       for (x = fields; x; prev = x, x = TREE_CHAIN (x))
  954.     {
  955.       if (DECL_NAME (x) == 0)
  956.         {
  957.           if (TREE_CODE (TREE_TYPE (x)) != UNION_TYPE)
  958.         continue;
  959.           TYPE_FIELDS (TREE_TYPE (x))
  960.         = delete_duplicate_fields_1 (field, 0, TYPE_FIELDS (TREE_TYPE (x)));
  961.           if (TYPE_FIELDS (TREE_TYPE (x)) == 0)
  962.         {
  963.           if (prev == 0)
  964.             fields = TREE_CHAIN (fields);
  965.           else
  966.             TREE_CHAIN (prev) = TREE_CHAIN (x);
  967.         }
  968.         }
  969.       else
  970.         {
  971.           if (DECL_NAME (field) == DECL_NAME (x))
  972.         {
  973.           if (TREE_CODE (field) == CONST_DECL
  974.               && TREE_CODE (x) == CONST_DECL)
  975.             error_with_decl (x, "duplicate enum value `%s'");
  976.           else if (TREE_CODE (field) == CONST_DECL
  977.                || TREE_CODE (x) == CONST_DECL)
  978.             error_with_decl (x, "duplicate field `%s' (as enum and non-enum)");
  979.           else
  980.             error_with_decl (x, "duplicate member `%s'");
  981.           if (prev == 0)
  982.             fields = TREE_CHAIN (fields);
  983.           else
  984.             TREE_CHAIN (prev) = TREE_CHAIN (x);
  985.         }
  986.         }
  987.     }
  988.     }
  989.   return fields;
  990. }
  991.  
  992. static void
  993. delete_duplicate_fields (fields)
  994.      tree fields;
  995. {
  996.   tree x;
  997.   for (x = fields; x && TREE_CHAIN (x); x = TREE_CHAIN (x))
  998.     TREE_CHAIN (x) = delete_duplicate_fields_1 (x, &x, TREE_CHAIN (x));
  999. }
  1000.  
  1001. /* Add OFFSET to all child types of T.
  1002.  
  1003.    OFFSET, which is a type offset, is number of bytes.
  1004.  
  1005.    Note that we don't have to worry about having two paths to the
  1006.    same base type, since this type owns its association list.  */
  1007. static void
  1008. propagate_basetype_offsets (for_type, t, offset)
  1009.      tree for_type, t;
  1010.      tree offset;
  1011. {
  1012.   int i, n_baselinks = CLASSTYPE_N_BASECLASSES (t);
  1013.  
  1014.   for (i = 1; i <= n_baselinks; i++)
  1015.     if (! CLASSTYPE_VIA_VIRTUAL (t, i))
  1016.       {
  1017.     tree basetype = CLASSTYPE_BASECLASS (t, i);
  1018.     tree assoc = assoc_value (TYPE_MAIN_VARIANT (basetype), for_type);
  1019.     if (CLASSTYPE_OFFSET (basetype) == integer_zero_node)
  1020.       basetype = build_classtype_variant (basetype, offset, 0);
  1021.     else
  1022.       basetype = build_classtype_variant (basetype,
  1023.                           genop (PLUS_EXPR, CLASSTYPE_OFFSET (basetype), offset), 0);
  1024.     /* Now make our own copy of this base type we can munge.  */
  1025.     basetype = copy_node (basetype);
  1026.     copy_type_lang_specific (basetype);
  1027.  
  1028.     CLASSTYPE_BASECLASS (t, i) = basetype;
  1029.     ASSOC_TYPE (assoc) = basetype;
  1030.     ASSOC_OFFSET (assoc) = CLASSTYPE_OFFSET (basetype);
  1031.     TYPE_NAME (basetype) = copy_node (TYPE_NAME (basetype));
  1032.     TREE_TYPE (TYPE_NAME (basetype)) = basetype;
  1033.     DECL_OFFSET (TYPE_NAME (basetype))
  1034.       = TREE_INT_CST_LOW (convert_units (CLASSTYPE_OFFSET (basetype), BITS_PER_UNIT, 1));
  1035.     propagate_basetype_offsets (for_type, basetype, offset);
  1036.     if (i < n_baselinks)
  1037.       {
  1038.         offset = genop (PLUS_EXPR,
  1039.                 convert_units (TYPE_SIZE (basetype), TYPE_SIZE_UNIT (basetype), BITS_PER_UNIT), offset);
  1040.       }
  1041.       }
  1042. }
  1043.  
  1044. /* Change the visibility of T::FDECL to VISIBILITY.
  1045.    Return 1 if change was legit, otherwise return 0.  */
  1046. static int
  1047. alter_visibility (t, fdecl, visibility)
  1048.      tree t;
  1049.      tree fdecl;
  1050.      enum visibility_type visibility;
  1051. {
  1052.   tree elem = purpose_member (t, DECL_VISIBILITY (fdecl));
  1053.   if (elem && TREE_VALUE (elem) != (tree)visibility)
  1054.     {
  1055.       if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL)
  1056.     {
  1057.       error_with_decl (TREE_TYPE (fdecl), "conflicting visibility specifications for method `%s', ignored");
  1058.     }
  1059.       else error ("conflicting visibility specifications for field `%s', ignored", IDENTIFIER_POINTER (DECL_NAME (fdecl)));
  1060.     }
  1061.   else if (TREE_PRIVATE (fdecl) && visibility != visibility_private)
  1062.     error_with_decl (fdecl, "cannot make private %s non-private");
  1063.   else if (TREE_PROTECTED (fdecl) && visibility == visibility_public)
  1064.             
  1065.     error_with_decl (fdecl, "cannot make protected %s public");
  1066.   else if (elem == NULL_TREE)
  1067.     {
  1068.       DECL_VISIBILITY (fdecl) = tree_cons (t, (tree)visibility,
  1069.                        DECL_VISIBILITY (fdecl));
  1070.       return 1;
  1071.     }
  1072.   return 0;
  1073. }
  1074.  
  1075. /* If TYPE does not have a constructor, then the compiler must
  1076.    manually deal with all of the initialization this type requires.
  1077.  
  1078.    If a base initializer exists only to fill in the virtual function
  1079.    table pointer, then we mark that fact with the TREE_VIRTUAL bit.
  1080.    This way, we avoid multiple initializations of the same field by
  1081.    each virtual function table up the class hierarchy.
  1082.  
  1083.    Virtual base class pointers are not initialized here.  They are
  1084.    initialized only at the "top level" of object creation.  If we
  1085.    initialized them here, we would have to skip a lot of work.  */
  1086.  
  1087. static void
  1088. build_class_init_list (type)
  1089.      tree type;
  1090. {
  1091.   tree base_init_list = NULL_TREE;
  1092.   tree member_init_list = NULL_TREE;
  1093.  
  1094.   /* Since we build member_init_list and base_init_list using
  1095.      tree_cons, backwards fields the all through work.  */
  1096.   tree x;
  1097.   int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (type);
  1098.  
  1099.   for (x = TYPE_FIELDS (type); x; x = TREE_CHAIN (x))
  1100.     {
  1101.       if (TREE_CODE (x) != FIELD_DECL)
  1102.     continue;
  1103.  
  1104.       if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (x))
  1105.       || DECL_INITIAL (x) != NULL_TREE)
  1106.     member_init_list = tree_cons (x, type, member_init_list);
  1107.     }
  1108.   member_init_list = nreverse (member_init_list);
  1109.  
  1110.   /* We will end up doing this last.  Need special marker
  1111.      to avoid infinite regress.  */
  1112.   if (TYPE_VIRTUAL_P (type))
  1113.     {
  1114.       base_init_list = build_tree_list (error_mark_node, type);
  1115.       if (CLASSTYPE_NEEDS_VIRTUAL_REINIT (type) == 0)
  1116.     TREE_VALUE (base_init_list) = NULL_TREE;
  1117.       TREE_ADDRESSABLE (base_init_list) = 1;
  1118.     }
  1119.  
  1120.   /* Each base class which needs to have initialization
  1121.      of some kind gets to make such requests known here.  */
  1122.   for (i = n_baseclasses; i > 0; i--)
  1123.     {
  1124.       tree basetype = CLASSTYPE_BASECLASS (type, i);
  1125.       tree blist;
  1126.  
  1127.       /* Don't initialize virtual baseclasses this way.  */
  1128.       if (TREE_VIA_VIRTUAL (basetype))
  1129.     continue;
  1130.  
  1131.       if (TYPE_HAS_CONSTRUCTOR (basetype))
  1132.     {
  1133.       base_init_list = tree_cons (NULL_TREE, basetype,
  1134.                       base_init_list);
  1135.       continue;
  1136.     }
  1137.  
  1138.       blist = CLASSTYPE_BASE_INIT_LIST (basetype);
  1139.       if (blist != NULL_TREE)
  1140.     {
  1141.       /* This is normally true for single inheritance.
  1142.          The win is we can shrink the chain of initializations
  1143.          to be done by only converting to the actual type
  1144.          we are interested in.  */
  1145.       if (TREE_VALUE (blist)
  1146.           && TREE_CODE (TREE_VALUE (blist)) == RECORD_TYPE
  1147.           && (DECL_OFFSET (TYPE_NAME (basetype))
  1148.           == DECL_OFFSET (TYPE_NAME (TREE_VALUE (blist)))))
  1149.         {
  1150.           if (base_init_list)
  1151.         {
  1152.           /* Does it do more than just fill in a
  1153.              virtual function table pointer?  */
  1154.           if (! TREE_ADDRESSABLE (blist))
  1155.             base_init_list = build_tree_list (blist, base_init_list);
  1156.           /* Can we get by just with the virtual function table
  1157.              pointer that it fills in?  */
  1158.           else if (TREE_ADDRESSABLE (base_init_list)
  1159.                && TREE_VALUE (base_init_list) == 0)
  1160.             base_init_list = blist;
  1161.           /* Maybe, but it is not obvious as the previous case.  */
  1162.           else if (! CLASSTYPE_NEEDS_VIRTUAL_REINIT (type))
  1163.             {
  1164.               tree last = tree_last (base_init_list);
  1165.               while (TREE_VALUE (last)
  1166.                  && TREE_CODE (TREE_VALUE (last)) == TREE_LIST)
  1167.             last = tree_last (TREE_VALUE (last));
  1168.               if (TREE_VALUE (last) == 0)
  1169.             base_init_list = build_tree_list (blist, base_init_list);
  1170.             }
  1171.         }
  1172.           else
  1173.         base_init_list = blist;
  1174.         }
  1175.       else
  1176.         {
  1177.           /* The function expand_aggr_init knows how to do the
  1178.          initialization of `basetype' without getting
  1179.          an explicit `blist'.  */
  1180.           if (base_init_list)
  1181.         base_init_list = tree_cons (NULL_TREE, basetype, base_init_list);
  1182.           else
  1183.         base_init_list = CLASSTYPE_AS_LIST (basetype);
  1184.         }
  1185.     }
  1186.     }
  1187.  
  1188.   if (base_init_list)
  1189.     if (member_init_list)
  1190.       CLASSTYPE_BASE_INIT_LIST (type) = build_tree_list (base_init_list, member_init_list);
  1191.     else
  1192.       CLASSTYPE_BASE_INIT_LIST (type) = base_init_list;
  1193.   else if (member_init_list)
  1194.     CLASSTYPE_BASE_INIT_LIST (type) = member_init_list;
  1195. }
  1196.  
  1197. struct base_info
  1198. {
  1199.   int has_virtual;
  1200.   int max_has_virtual;
  1201.   int n_ancestors;
  1202.   tree vfield;
  1203.   tree vfields;
  1204.   char needs_default_ctor;
  1205.   char cant_have_default_ctor;
  1206.   char needs_const_ctor;
  1207.   char cant_have_const_ctor;
  1208. };
  1209.  
  1210. /* Record information about type T derived from its base classes.
  1211.    Store most of that information in T itself, and place the
  1212.    remaining information in the struct BASE_INFO.
  1213.  
  1214.    Returns the index of the first base class to have virtual functions,
  1215.    or zero if no such base class.  */
  1216.  
  1217. static int
  1218. finish_base_struct (t, b)
  1219.      tree t;
  1220.      struct base_info *b;
  1221. {
  1222.   int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
  1223.   int first_vfn_base_index = 0;
  1224.  
  1225.   bzero (b, sizeof (struct base_info));
  1226.  
  1227.   for (i = 1; i <= n_baseclasses; i++)
  1228.     {
  1229.       tree basetype = CLASSTYPE_BASECLASS (t, i);
  1230.  
  1231.       /* If the type of basetype is incomplete, then
  1232.      we already complained about that fact
  1233.      (and we should have fixed it up as well).  */
  1234.       if (TYPE_SIZE (basetype) == 0)
  1235.     {
  1236.       int j;
  1237.       /* The base type is of incomplete type.  It is
  1238.          probably best to pretend that it does not
  1239.          exist.  */
  1240.       if (i == n_baseclasses)
  1241.         CLASSTYPE_BASECLASS (t, i) = NULL_TREE;
  1242.       CLASSTYPE_N_BASECLASSES (t) -= 1;
  1243.       n_baseclasses -= 1;
  1244.       for (j = i; j < n_baseclasses; j++)
  1245.         {
  1246.           CLASSTYPE_BASECLASS (t, j) = CLASSTYPE_BASECLASS (t, j+1);
  1247.           SET_CLASSTYPE_VIAS (t, j,
  1248.                   CLASSTYPE_VIA_PUBLIC (t, j+1),
  1249.                   CLASSTYPE_VIA_VIRTUAL (t, j+1));
  1250.         }
  1251.     }
  1252.  
  1253.       if (TYPE_WRAP_TYPE (t) == NULL_TREE)
  1254.     TYPE_WRAP_TYPE (t) = TYPE_WRAP_TYPE (basetype);
  1255.       else if (TYPE_WRAP_TYPE (basetype)
  1256.            && TYPE_WRAP_TYPE (t) != TYPE_WRAP_TYPE (basetype))
  1257.     /* Must have its own.  */
  1258.     TYPE_WRAP_TYPE (t) = error_mark_node;
  1259.  
  1260.       if (TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype))
  1261.     b->needs_default_ctor = 1;
  1262.       else if (TYPE_HAS_CONSTRUCTOR (basetype))
  1263.     b->cant_have_default_ctor = 1;
  1264.       if (TYPE_GETS_CONST_INIT_REF (basetype))
  1265.     b->needs_const_ctor = 1;
  1266.       else if (TYPE_GETS_INIT_REF (basetype))
  1267.     b->cant_have_const_ctor = 1;
  1268.  
  1269.       CLASSTYPE_ALTERS_VISIBILITIES_P (t)
  1270.     |= CLASSTYPE_ALTERS_VISIBILITIES_P (basetype);
  1271.  
  1272.       b->n_ancestors += CLASSTYPE_N_SUPERCLASSES (basetype);
  1273.       TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype);
  1274.       TYPE_NEEDS_CONSTRUCTOR (t) |= TYPE_NEEDS_CONSTRUCTOR (basetype);
  1275.       TYPE_NEEDS_DESTRUCTOR (t) |= TYPE_NEEDS_DESTRUCTOR (basetype);
  1276.       TYPE_ANY_ASSIGNS_THIS (t) |= TYPE_ANY_ASSIGNS_THIS (basetype);
  1277.       TYPE_GETS_ASSIGNMENT (t) |= TYPE_GETS_ASSIGNMENT (basetype);
  1278.       TYPE_GETS_INIT_REF (t) |= TYPE_GETS_INIT_REF (basetype);
  1279.  
  1280.       TYPE_OVERLOADS_CALL_EXPR (t) |= TYPE_OVERLOADS_CALL_EXPR (basetype);
  1281.       TYPE_OVERLOADS_ARRAY_REF (t) |= TYPE_OVERLOADS_ARRAY_REF (basetype);
  1282.  
  1283.       if (CLASSTYPE_OFFSET (basetype) != integer_zero_node)
  1284.     {
  1285.       /* Completely unshare potentially shared data, and
  1286.          update what is ours.  */
  1287.       tree assoc = assoc_value (TYPE_MAIN_VARIANT (basetype), t);
  1288.       basetype = copy_node (basetype);
  1289.       copy_type_lang_specific (basetype);
  1290.       CLASSTYPE_BASECLASS (t, i) = basetype;
  1291.       ASSOC_TYPE (assoc) = basetype;
  1292.  
  1293.       /* Propagate this offset through all the children.  Do this
  1294.          before uniquizing baseclasses for virtual functions.  */
  1295.       CLASSTYPE_ASSOC (basetype) = copy_assoc (CLASSTYPE_ASSOC (TYPE_MAIN_VARIANT (basetype)));
  1296.  
  1297.       propagate_basetype_offsets (basetype, basetype, CLASSTYPE_OFFSET (basetype));
  1298.     }
  1299.  
  1300.       if (! CLASSTYPE_VIA_VIRTUAL (t, i))
  1301.     CLASSTYPE_N_SUPERCLASSES (t) += 1;
  1302.  
  1303.       if (TYPE_VIRTUAL_P (basetype))
  1304.     {
  1305.       if (CLASSTYPE_VSIZE (basetype) > b->max_has_virtual)
  1306.         b->max_has_virtual = CLASSTYPE_VSIZE (basetype);
  1307.  
  1308.       /* Don't borrow virtuals from virtual baseclasses.  */
  1309.       if (TREE_VIA_VIRTUAL (basetype))
  1310.         continue;
  1311.  
  1312.       if (first_vfn_base_index == 0)
  1313.         {
  1314.           first_vfn_base_index = i;
  1315.  
  1316.           b->has_virtual = CLASSTYPE_VSIZE (basetype);
  1317.           b->vfield = CLASSTYPE_VFIELD (basetype);
  1318.           b->vfields = CLASSTYPE_VFIELDS (basetype);
  1319.           CLASSTYPE_VFIELD (t) = b->vfield;
  1320.         }
  1321.       else
  1322.         {
  1323.           /* Only add unique vslots, and flatten them out as we go.  */
  1324.           tree vslots = CLASSTYPE_VFIELDS (basetype);
  1325.           while (vslots)
  1326.         {
  1327.           if (TREE_PURPOSE (vslots) == NULL_TREE
  1328.               || ! TREE_VIA_VIRTUAL (TREE_PURPOSE (vslots)))
  1329.             {
  1330.               tree value = TREE_VALUE (vslots);
  1331.               if (TYPE_MAIN_VARIANT (basetype) == value)
  1332.             b->vfields = tree_cons (basetype, value, b->vfields);
  1333.               else
  1334.             b->vfields = tree_cons (get_base_type (value, basetype, 0),
  1335.                         value, b->vfields);
  1336.             }
  1337.           vslots = TREE_CHAIN (vslots);
  1338.         }
  1339.  
  1340.           if (b->has_virtual == 0)
  1341.         {
  1342.           first_vfn_base_index = i;
  1343.           b->has_virtual = CLASSTYPE_VSIZE (basetype);
  1344.           b->vfield = CLASSTYPE_VFIELD (basetype);
  1345.           CLASSTYPE_VFIELD (t) = b->vfield;
  1346.         }
  1347.         }
  1348.     }
  1349.     }
  1350.   if (b->vfield == 0)
  1351.     /* If all virtual functions come only from virtual baseclasses.  */
  1352.     return 0;
  1353.   return first_vfn_base_index;
  1354. }
  1355.  
  1356. /* Set memoizing fields and bits of T (and its variants) for later use.
  1357.    FIRST_VFN_BASE_INDEX is the first baseclass of T with virtual functions.
  1358.    MAX_HAS_VIRTUAL is the largest size of any T's virtual function tables.  */
  1359. static void
  1360. finish_struct_bits (t, first_vfn_base_index, max_has_virtual)
  1361.      tree t;
  1362.      int first_vfn_base_index, max_has_virtual;
  1363. {
  1364.   int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
  1365.   tree method_vec = CLASSTYPE_METHOD_VEC (t);
  1366.  
  1367.   /* Fix up variants (if any).  */
  1368.   tree variants = TYPE_NEXT_VARIANT (t);
  1369.   while (variants)
  1370.     {
  1371.       TYPE_NEEDS_CONSTRUCTOR (variants) = TYPE_NEEDS_CONSTRUCTOR (t);
  1372.       TYPE_NEEDS_CONSTRUCTING (variants) = TYPE_NEEDS_CONSTRUCTING (t);
  1373.       TYPE_NEEDS_DESTRUCTOR (variants) = TYPE_NEEDS_DESTRUCTOR (t);
  1374.       variants = TYPE_NEXT_VARIANT (variants);
  1375.     }
  1376.  
  1377.   if (n_baseclasses && max_has_virtual)
  1378.     {
  1379.       /* Done by `finish_struct' for classes without baseclasses.  */
  1380.       int has_abstract_virtuals = CLASSTYPE_ABSTRACT_VIRTUALS (t) != 0;
  1381.       if (has_abstract_virtuals == 0)
  1382.     for (i = CLASSTYPE_N_BASECLASSES (t); i >= 1; i--)
  1383.       has_abstract_virtuals
  1384.         |= (CLASSTYPE_ABSTRACT_VIRTUALS (CLASSTYPE_BASECLASS (t, i)) != 0);
  1385.       if (has_abstract_virtuals)
  1386.     CLASSTYPE_ABSTRACT_VIRTUALS (t) = get_abstract_virtuals (t);
  1387.     }
  1388.  
  1389.   if (n_baseclasses)
  1390.     {
  1391.       /* Notice whether this class has type conversion functions defined.
  1392.      Also report whether joining two types yields an ambiguity in the
  1393.      virtual function table, e.g.,
  1394.      
  1395.      struct A { virtual int f (); };
  1396.      struct B { virtual int f (); };
  1397.      struct C : A, B { / * no f (); * / };    / / error, ambiguous
  1398.      */
  1399.       tree basetypes = CLASSTYPE_VBASECLASSES (t), basetype;
  1400.       int n_vbases = list_length (basetypes), j;
  1401.  
  1402.       build_mi_virtuals (n_baseclasses + (n_vbases*n_baseclasses), max_has_virtual);
  1403.       /* Fill in virutal function table with values which do not come
  1404.      "normal"ly, i.e., those which come from virtual and/or
  1405.      non-leftmost base classes.  */
  1406.       for (i = n_baseclasses; basetypes; basetypes = TREE_CHAIN (basetypes))
  1407.     {
  1408.       basetype = TREE_VALUE (basetypes);
  1409.       if (CLASSTYPE_VSIZE (basetype))
  1410.         for (j = n_baseclasses; j > 0; j--)
  1411.           {
  1412.         tree this_base = CLASSTYPE_BASECLASS (t, j);
  1413.         if (get_base_distance (basetype, TYPE_MAIN_VARIANT (this_base), 0, 0) != -1)
  1414.           add_mi_virtuals (++i, TREE_CHAIN (ASSOC_VIRTUALS (basetypes)));
  1415.           }
  1416.     }
  1417.       for (i = n_baseclasses; i > 0; i--)
  1418.     {
  1419.       basetype = CLASSTYPE_BASECLASS (t, i);
  1420.  
  1421.       if (TYPE_HAS_CONVERSION (basetype))
  1422.         {
  1423.           TYPE_HAS_CONVERSION (t) = 1;
  1424.           TYPE_HAS_INT_CONVERSION (t) |= TYPE_HAS_INT_CONVERSION (basetype);
  1425.           TYPE_HAS_REAL_CONVERSION (t) |= TYPE_HAS_REAL_CONVERSION (basetype);
  1426.         }
  1427.       if (TREE_VIA_VIRTUAL (basetype))
  1428.         /* Virtual functions from virtual baseclasses are done above.  */;
  1429.       else if (i == first_vfn_base_index)
  1430.         add_mi_virtuals (i, TREE_CHAIN (CLASS_ASSOC_VIRTUALS (t)));
  1431.       else if (CLASSTYPE_VSIZE (basetype) != 0)
  1432.         add_mi_virtuals (i, TREE_CHAIN (CLASS_ASSOC_VIRTUALS (basetype)));
  1433.     }
  1434.       report_ambiguous_mi_virtuals (n_baseclasses + (n_vbases*n_baseclasses), t);
  1435.  
  1436.       /* Now that we know what the virtual functiond table looks like,
  1437.      fix up offsets in the presence of virtual base classes.  */
  1438.       if (n_vbases)
  1439.     fixup_vbase_offsets (t);
  1440.     }
  1441.  
  1442.   if (TYPE_HAS_CONVERSION (t))
  1443.     {
  1444.       tree first_ptr_conversion = NULL_TREE, last_ptr_conversion;
  1445.       tree first_constptr_conversion = NULL_TREE, last_constptr_conversion;
  1446.       tree *tmp = &TREE_VEC_ELT (method_vec, 1);
  1447.       int need_int = ! TYPE_HAS_INT_CONVERSION (t);
  1448.       int need_real = ! TYPE_HAS_REAL_CONVERSION (t);
  1449.  
  1450.       while (tmp != TREE_VEC_END (method_vec))
  1451.     {
  1452.       if (OPERATOR_TYPENAME_P (DECL_ORIGINAL_NAME (*tmp)))
  1453.         {
  1454.           tree fntype = TREE_TYPE (*tmp);
  1455.           tree return_type = TREE_TYPE (fntype);
  1456.           assert (TREE_CODE (fntype) == METHOD_TYPE);
  1457.  
  1458.           if (TREE_CODE (return_type) == POINTER_TYPE
  1459.           || (TREE_CODE (return_type) == REFERENCE_TYPE
  1460.               && TREE_CODE (TREE_TYPE (return_type)) == POINTER_TYPE))
  1461.         {
  1462.           if (TREE_READONLY (TREE_TYPE (return_type)))
  1463.             {
  1464.               if (first_constptr_conversion == NULL_TREE)
  1465.             first_constptr_conversion = *tmp;
  1466.               last_constptr_conversion = *tmp;
  1467.             }
  1468.           else
  1469.             {
  1470.               if (first_ptr_conversion == NULL_TREE)
  1471.             first_ptr_conversion = *tmp;
  1472.               last_ptr_conversion = *tmp;
  1473.             }
  1474.         }
  1475.           else if (need_int
  1476.                && (TREE_CODE (return_type) == INTEGER_TYPE
  1477.                || (TREE_CODE (return_type) == REFERENCE_TYPE
  1478.                    && TREE_CODE (TREE_TYPE (return_type)) == INTEGER_TYPE)))
  1479.         {
  1480.           TYPE_HAS_INT_CONVERSION (t) = 1;
  1481.           need_int = 0;
  1482.           if (need_real == 0)
  1483.             break;
  1484.         }
  1485.           else if (need_real
  1486.                && (TREE_CODE (return_type) == REAL_TYPE
  1487.                || (TREE_CODE (return_type) == REFERENCE_TYPE
  1488.                    && TREE_CODE (TREE_TYPE (return_type)) == REAL_TYPE)))
  1489.         {
  1490.           TYPE_HAS_REAL_CONVERSION (t) = 1;
  1491.           need_real = 0;
  1492.           if (need_int == 0)
  1493.             break;
  1494.         }
  1495.         }
  1496.       tmp++;
  1497.     }
  1498.       if (first_ptr_conversion)
  1499.     {
  1500.       if (first_ptr_conversion != last_ptr_conversion)
  1501.         CLASSTYPE_PTR_CONVERSION (t) = error_mark_node;
  1502.       else
  1503.         CLASSTYPE_PTR_CONVERSION (t) = first_ptr_conversion;
  1504.     }
  1505.       else
  1506.     CLASSTYPE_PTR_CONVERSION (t) = NULL_TREE;
  1507.       if (first_constptr_conversion)
  1508.     {
  1509.       if (first_constptr_conversion != last_constptr_conversion)
  1510.         CLASSTYPE_CONSTPTR_CONVERSION (t) = error_mark_node;
  1511.       else
  1512.         CLASSTYPE_CONSTPTR_CONVERSION (t) = first_constptr_conversion;
  1513.     }
  1514.       else
  1515.     CLASSTYPE_CONSTPTR_CONVERSION (t) = NULL_TREE;
  1516.     }
  1517.  
  1518.   /* If this type has constructors, force its mode to be BLKmode,
  1519.      and force its TREE_ADDRESSABLE bit to be nonzero.  */
  1520.   if (TYPE_NEEDS_CONSTRUCTING (t) || TYPE_NEEDS_DESTRUCTOR (t))
  1521.     {
  1522.       tree variants = t;
  1523.  
  1524.       if (TREE_CODE (TYPE_NAME (t)) == TYPE_DECL)
  1525.     DECL_MODE (TYPE_NAME (t)) = BLKmode;
  1526.       while (variants)
  1527.     {
  1528.       TYPE_MODE (variants) = BLKmode;
  1529.       TREE_ADDRESSABLE (variants) = 1;
  1530.       variants = TYPE_NEXT_VARIANT (variants);
  1531.     }
  1532.     }
  1533. }
  1534.  
  1535. /* Create a RECORD_TYPE or UNION_TYPE node for a C struct or union declaration
  1536.    (or C++ class declaration).
  1537.  
  1538.    For C++, we must handle the building of derived classes.
  1539.    Also, C++ allows static class members.  The way that this is
  1540.    handled is to keep the field name where it is (as the DECL_NAME
  1541.    of the field), and place the overloaded decl in the DECL_OFFSET
  1542.    of the field.  layout_record and layout_union will know about this.
  1543.  
  1544.    More C++ hair: inline functions have text in their
  1545.    DECL_PENDING_INLINE_INFO nodes which must somehow be parsed into
  1546.    meaningful tree structure.  After the struct has been laid out, set
  1547.    things up so that this can happen.
  1548.  
  1549.    And still more: virtual functions.  In the case of single inheritance,
  1550.    when a new virtual function is seen which redefines a virtual function
  1551.    from the base class, the new virtual function is placed into
  1552.    the virtual function table at exactly the same address that
  1553.    it had in the base class.  When this is extended to multiple
  1554.    inheritance, the same thing happens, except that multiple virtual
  1555.    function tables must be maintained.  The first virtual function
  1556.    table is treated in exactly the same way as in the case of single
  1557.    inheritance.  Additional virtual function tables have different
  1558.    DELTAs, which tell how to adjust `this' to point to the right thing.
  1559.  
  1560.    LIST_OF_FIELDLISTS is just that.  The elements of the list are
  1561.    TREE_LIST elements, whose TREE_PURPOSE field tells what visibility
  1562.    the list has, and the TREE_VALUE slot gives the actual fields.
  1563.  
  1564.    EMPTY is non-zero if this structure has no declarations following it.
  1565.  
  1566.    If flag_all_virtual == 1, then we lay all functions into
  1567.    the virtual function table, as though they were declared
  1568.    virtual.  Constructors do not lay down in the virtual function table.
  1569.  
  1570.    If flag_all_virtual == 2, then we lay all functions into
  1571.    the virtual function table, such that virtual functions
  1572.    occupy a space by themselves, and then all functions
  1573.    of the class occupy a space by themselves.  This is illustrated
  1574.    in the following diagram:
  1575.  
  1576.    class A; class B : A;
  1577.  
  1578.     Class A's vtbl:            Class B's vtbl:
  1579.     --------------------------------------------------------------------
  1580.    | A's virtual functions|        | B's virtual funcitions    |
  1581.    |              |        | (may inherit some from A).    |
  1582.     --------------------------------------------------------------------
  1583.    | All of A's functions |        | All of A's functions        |
  1584.    | (such as a->A::f).      |        | (such as b->A::f)        |
  1585.     --------------------------------------------------------------------
  1586.                     | B's new virtual functions    |
  1587.                     | (not defined in A.)        |
  1588.                      -------------------------------
  1589.                     | All of B's functions        |
  1590.                     | (such as b->B::f)        |
  1591.                      -------------------------------
  1592.  
  1593.    this allows the program to make references to any function, virtual
  1594.    or otherwise in a type-consistant manner.  */
  1595.  
  1596. tree
  1597. finish_struct (t, list_of_fieldlists, empty, warn_anon)
  1598.      tree t;
  1599.      tree list_of_fieldlists;
  1600.      int empty;
  1601.      int warn_anon;
  1602. {
  1603.   int old;
  1604.   int round_up_size = 1;
  1605.   /* Set non-zero to debug using default functions.
  1606.      Not set by program.  */
  1607.   static int debug_default_functions = 0;
  1608.  
  1609.   enum tree_code code = TREE_CODE (t);
  1610.   register tree x, y, method_vec;
  1611.   int needs_ctor = 0, needs_dtor = 0;
  1612.   int members_need_dtors = 0;
  1613.   tree name = TYPE_NAME (t), fields, fn_fields, tail;
  1614.   enum visibility_type visibility;
  1615.   int all_virtual;
  1616.   int has_virtual;
  1617.   int max_has_virtual;
  1618.   tree pending_virtuals = NULL_TREE;
  1619.   tree abstract_virtuals = NULL_TREE;
  1620.   tree vfield;
  1621.   tree vfields;
  1622.   int needs_default_ctor;
  1623.   int cant_have_default_ctor;
  1624.   int needs_const_ctor;
  1625.   int cant_have_const_ctor;
  1626.  
  1627.   /* The index of the first base class which has virtual
  1628.      functions.  Only applied to non-virtual baseclasses.  */
  1629.   int first_vfn_base_index;
  1630.  
  1631.   int i, n_baseclasses;
  1632.   int any_default_members = 0;
  1633.   char *err_name;
  1634.   int const_sans_init = 0;
  1635.   int ref_sans_init = 0;
  1636.   int nonprivate_method = 0;
  1637.  
  1638.   if (TREE_CODE (name) == TYPE_DECL)
  1639.     name = DECL_NAME (name);
  1640.   err_name = IDENTIFIER_POINTER (name);
  1641.  
  1642.   if (warn_anon && code != UNION_TYPE && ANON_AGGRNAME_P (name))
  1643.     {
  1644.       warning ("un-usable class ignored (anonymous classes and unions are useless)");
  1645.       err_name = "(anon)";
  1646.     }
  1647.  
  1648.   leftmost_baseclasses = NULL_TREE;
  1649.   if (TYPE_SIZE (t))
  1650.     {
  1651.       if (TREE_CODE (t) == UNION_TYPE)
  1652.     error ("redefinition of `union %s'", err_name);
  1653.       else if (TREE_CODE (t) == RECORD_TYPE)
  1654.     error ("redefinition of `struct %s'", err_name);
  1655.       else
  1656.     assert (0);
  1657.       popclass (0);
  1658.       return t;
  1659.     }
  1660.  
  1661.   /* If this type was previously laid out as a forward reference,
  1662.      make sure we lay it out again.  */
  1663.  
  1664.   TYPE_SIZE (t) = 0;
  1665.   CLASSTYPE_GOT_SEMICOLON (t) = 0;
  1666.  
  1667.   old = suspend_momentary ();
  1668.  
  1669.   /* Install struct as DECL_FIELD_CONTEXT of each field decl.
  1670.      Also process specified field sizes.
  1671.      Set DECL_SIZE_UNIT to the specified size, or 0 if none specified.
  1672.      The specified size is found in the DECL_INITIAL.
  1673.      Store 0 there, except for ": 0" fields (so we can find them
  1674.      and delete them, below).  */
  1675.  
  1676.   n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
  1677.   if (n_baseclasses >= 1)
  1678.     {
  1679.       struct base_info base_info;
  1680.  
  1681.       /* If using multiple inheritance, this may cause variants of our
  1682.      basetypes to be used (instead of their canonical forms).  */
  1683.       fields = layout_basetypes (t);
  1684.       y = tree_last (fields);
  1685.  
  1686.       first_vfn_base_index = finish_base_struct (t, &base_info);
  1687.       has_virtual = base_info.has_virtual;
  1688.       max_has_virtual = base_info.max_has_virtual;
  1689.       CLASSTYPE_N_SUPERCLASSES (t) += base_info.n_ancestors;
  1690.       vfield = base_info.vfield;
  1691.       vfields = base_info.vfields;
  1692.       needs_default_ctor = base_info.needs_default_ctor;
  1693.       cant_have_default_ctor = base_info.cant_have_default_ctor;
  1694.       needs_const_ctor = base_info.needs_const_ctor;
  1695.       cant_have_const_ctor = base_info.cant_have_const_ctor;
  1696.       n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
  1697.     }
  1698.   else
  1699.     {
  1700.       first_vfn_base_index = 0;
  1701.       has_virtual = 0;
  1702.       max_has_virtual = 0;
  1703.       vfield = NULL_TREE;
  1704.       vfields = NULL_TREE;
  1705.       fields = NULL_TREE;
  1706.       y = NULL_TREE;
  1707.       needs_default_ctor = 0;
  1708.       cant_have_default_ctor = 0;
  1709.       needs_const_ctor = 0;
  1710.       cant_have_const_ctor = 0;
  1711.     }
  1712.  
  1713.   /* The three of these are approximations which may later be
  1714.      modified.  Needed at this point to make add_virtual_function
  1715.      and modify_vtable_entries work.  */
  1716.   CLASSTYPE_ASSOC (t) = make_assoc (integer_zero_node, t,
  1717.                     0, 0, CLASSTYPE_ASSOC (t));
  1718.   CLASSTYPE_VFIELDS (t) = vfields;
  1719.   CLASSTYPE_VFIELD (t) = vfield;
  1720.  
  1721.   fn_fields = NULL_TREE;
  1722.   tail = NULL_TREE;
  1723.   if (y && list_of_fieldlists)
  1724.     TREE_CHAIN (y) = TREE_VALUE (list_of_fieldlists);
  1725.  
  1726. #ifdef SOS
  1727.   if (flag_all_virtual == 2)
  1728.     all_virtual = 2;
  1729.   else
  1730. #endif
  1731.     {
  1732.       if (flag_all_virtual == 1 && TYPE_OVERLOADS_METHOD_CALL_EXPR (t))
  1733.     all_virtual = 1;
  1734.       else
  1735.     all_virtual = 0;
  1736.     }
  1737.  
  1738.   if (CLASSTYPE_DECLARED_CLASS (t) == 0)
  1739.     {
  1740.       nonprivate_method = 1;
  1741.       if (list_of_fieldlists
  1742.       && TREE_PURPOSE (list_of_fieldlists) == (tree)visibility_default)
  1743.     TREE_PURPOSE (list_of_fieldlists) = (tree)visibility_public;
  1744.     }
  1745.   else if (list_of_fieldlists
  1746.        && TREE_PURPOSE (list_of_fieldlists) == (tree)visibility_default)
  1747.     TREE_PURPOSE (list_of_fieldlists) = (tree)visibility_private;
  1748.  
  1749.   while (list_of_fieldlists)
  1750.     {
  1751.       visibility = (enum visibility_type)TREE_PURPOSE (list_of_fieldlists);
  1752.  
  1753.       for (x = TREE_VALUE (list_of_fieldlists); x; x = TREE_CHAIN (x))
  1754.     {
  1755.       TREE_PRIVATE (x) = visibility == visibility_private;
  1756.       TREE_PROTECTED (x) = visibility == visibility_protected;
  1757.       if (TREE_CODE (x) == FUNCTION_DECL)
  1758.         {
  1759.           /* Clear out this flag.
  1760.  
  1761.              @@ Doug may figure out how to break
  1762.          @@ this with nested classes and friends.  */
  1763.           DECL_IN_AGGR_P (x) = 0;
  1764.  
  1765.           nonprivate_method |= ! TREE_PRIVATE (x);
  1766.  
  1767.           /* If this was an evil function, don't keep it in class.  */
  1768.           if (IDENTIFIER_ERROR_LOCUS (DECL_NAME (x)))
  1769.         continue;
  1770.  
  1771.           if (y) TREE_CHAIN (y) = TREE_CHAIN (x);
  1772.           if (! fn_fields) fn_fields = x;
  1773.           else TREE_CHAIN (tail) = x;
  1774.           tail = x;
  1775.           if (DECL_CONTEXT (x))
  1776.         continue;
  1777.  
  1778.           DECL_CONTEXT (x) = t;
  1779.           DECL_VCONTEXT (x) = t;
  1780.  
  1781.           DECL_SIZE_UNIT (x) = 0;
  1782.  
  1783.           /* The name of the field is the original field name
  1784.          Save this in auxiliary field for later overloading.  */
  1785.           if (DECL_VIRTUAL_P (x)
  1786.           || (all_virtual == 1 && ! DECL_CONSTRUCTOR_P (x)))
  1787.         {
  1788.           pending_virtuals = add_virtual_function (pending_virtuals,
  1789.                                &has_virtual, x,
  1790.                                first_vfn_base_index);
  1791.           if (DECL_ABSTRACT_VIRTUAL_P (x))
  1792.             abstract_virtuals = tree_cons (NULL_TREE, x, abstract_virtuals);
  1793.         }
  1794.           continue;
  1795.         }
  1796.  
  1797.       /* Handle visibility declarations.  */
  1798.       if (DECL_NAME (x) && TREE_CODE (DECL_NAME (x)) == SCOPE_REF)
  1799.         {
  1800.           tree fdecl = TREE_OPERAND (DECL_NAME (x), 1);
  1801.  
  1802.           if (y) TREE_CHAIN (y) = TREE_CHAIN (x);
  1803.           /* Make type T see field decl FDECL with
  1804.          the visibility VISIBILITY.  */
  1805.           if (TREE_CODE (fdecl) == TREE_LIST)
  1806.         {
  1807.           fdecl = TREE_VALUE (fdecl);
  1808.           while (fdecl)
  1809.             {
  1810.               if (alter_visibility (t, fdecl, visibility) == 0)
  1811.             break;
  1812.               fdecl = TREE_CHAIN (fdecl);
  1813.             }
  1814.         }
  1815.           else alter_visibility (t, fdecl, visibility);
  1816.           CLASSTYPE_ALTERS_VISIBILITIES_P (t) = 1;
  1817.           continue;
  1818.         }
  1819.  
  1820.       /* Perform error checking that did not get done in grokdeclarator.  */
  1821.       if (TREE_CODE (TREE_TYPE (x)) == FUNCTION_TYPE)
  1822.         {
  1823.           error_with_decl (x, "field `%s' invalidly declared function type");
  1824.           TREE_TYPE (x) = build_pointer_type (TREE_TYPE (x));
  1825.         }
  1826.       else if (TREE_CODE (TREE_TYPE (x)) == METHOD_TYPE)
  1827.         {
  1828.           error_with_decl (x, "field `%s' invalidly declared method type");
  1829.           TREE_TYPE (x) = build_pointer_type (TREE_TYPE (x));
  1830.         }
  1831.       else if (TREE_CODE (TREE_TYPE (x)) == OFFSET_TYPE)
  1832.         {
  1833.           error_with_decl (x, "field `%s' invalidly declared offset type");
  1834.           TREE_TYPE (x) = build_pointer_type (TREE_TYPE (x));
  1835.         }
  1836.       /* If this is of reference type, check if it needs an init.  */
  1837.       if (TREE_CODE (TREE_TYPE (x)) == REFERENCE_TYPE
  1838.           && DECL_INITIAL (x) == 0)
  1839.         ref_sans_init = 1;
  1840.  
  1841.       /* When this goes into scope, it will be a non-local reference.  */
  1842.       TREE_NONLOCAL (x) = 1;
  1843.  
  1844.       if (TREE_CODE (x) == FIELD_DECL)
  1845.         {
  1846.           /* Never let anything with uninheritable virutals
  1847.          make it through without complaint.  */
  1848.           if (TYPE_LANG_SPECIFIC (TREE_TYPE (x))
  1849.           && CLASSTYPE_ABSTRACT_VIRTUALS (TREE_TYPE (x)))
  1850.         abstract_virtuals_error (x, TREE_TYPE (x));
  1851.  
  1852.           if (TYPE_LANG_SPECIFIC (TREE_TYPE (x)))
  1853.         {
  1854.           if (TYPE_HAS_DEFAULT_CONSTRUCTOR (TREE_TYPE (x)))
  1855.             needs_default_ctor = 1;
  1856.           if (TYPE_GETS_CONST_INIT_REF (TREE_TYPE (x)))
  1857.             needs_const_ctor = 1;
  1858.           else if (TYPE_GETS_INIT_REF (TREE_TYPE (x)))
  1859.             cant_have_const_ctor = 1;
  1860.         }
  1861.           else if (DECL_INITIAL (x) == NULL_TREE
  1862.                && (TYPE_HAS_CONSTRUCTOR (TREE_TYPE (x))
  1863.                || TREE_CODE (TREE_TYPE (x)) == REFERENCE_TYPE))
  1864.         cant_have_default_ctor = 1;
  1865.  
  1866.           /* If any field is const, the structure type is pseudo-const.  */
  1867.           if (TREE_READONLY (x))
  1868.         {
  1869.           C_TYPE_FIELDS_READONLY (t) = 1;
  1870.           if (DECL_INITIAL (x) == 0)
  1871.             const_sans_init = 1;
  1872.         }
  1873.           else 
  1874.         {
  1875.           /* A field that is pseudo-const makes the structure likewise.  */
  1876.           tree t1 = TREE_TYPE (x);
  1877.           while (TREE_CODE (t1) == ARRAY_TYPE)
  1878.             t1 = TREE_TYPE (t1);
  1879.           if (IS_AGGR_TYPE (t1))
  1880.             {
  1881.               if (C_TYPE_FIELDS_READONLY (t1))
  1882.             C_TYPE_FIELDS_READONLY (t) = 1;
  1883.               if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (t1))
  1884.             const_sans_init = 1;
  1885.             }
  1886.         }
  1887.         }
  1888.       else if (TREE_STATIC (x) && TREE_CODE (t) == UNION_TYPE)
  1889.         /* Unions cannot have static members.  */
  1890.         error_with_decl (x, "field `%s' declared static in union");
  1891.  
  1892.       if (! fields) fields = x;
  1893.       DECL_FIELD_CONTEXT (x) = t;
  1894.       DECL_SIZE_UNIT (x) = 0;
  1895.  
  1896.       if (TREE_PACKED (x))
  1897.         {
  1898.           /* Invalid bit-field size done by grokfield.  */
  1899.           /* Detect invalid bit-field type.  */
  1900.           if (DECL_INITIAL (x)
  1901.           && TREE_CODE (TREE_TYPE (x)) != INTEGER_TYPE
  1902.           && TREE_CODE (TREE_TYPE (x)) != ENUMERAL_TYPE)
  1903.         {
  1904.           error_with_decl (x, "bit-field `%s' has invalid type");
  1905.           DECL_INITIAL (x) = NULL;
  1906.         }
  1907.           if (DECL_INITIAL (x) && pedantic
  1908.           && TREE_TYPE (x) != integer_type_node
  1909.           && TREE_TYPE (x) != unsigned_type_node)
  1910.         warning_with_decl (x, "bit-field `%s' type invalid in ANSI C");
  1911.  
  1912.           /* Detect and ignore out of range field width.  */
  1913.           if (DECL_INITIAL (x))
  1914.         {
  1915.           register int width = TREE_INT_CST_LOW (DECL_INITIAL (x));
  1916.  
  1917.           if (width < 0)
  1918.             {
  1919.               DECL_INITIAL (x) = NULL;
  1920.               warning_with_decl (x, "negative width in bit-field `%s'");
  1921.             }
  1922.           else if (width == 0 && DECL_NAME (x) != 0)
  1923.             {
  1924.               error_with_decl (x, "zero width for bit-field `%s'");
  1925.               DECL_INITIAL (x) = NULL;
  1926.             }
  1927.           else if (width > TYPE_PRECISION (TREE_TYPE (x)))
  1928.             {
  1929.               DECL_INITIAL (x) = NULL;
  1930.               warning_with_decl (x, "width of `%s' exceeds its type");
  1931.             }
  1932.         }
  1933.  
  1934.           /* Process valid field width.  */
  1935.           if (DECL_INITIAL (x))
  1936.         {
  1937.           register int width = TREE_INT_CST_LOW (DECL_INITIAL (x));
  1938.  
  1939.           if (width == 0)
  1940.             {
  1941.               /* field size 0 => mark following field as "aligned" */
  1942.               if (TREE_CHAIN (x))
  1943.             DECL_ALIGN (TREE_CHAIN (x))
  1944.               = MAX (DECL_ALIGN (TREE_CHAIN (x)), EMPTY_FIELD_BOUNDARY);
  1945.               /* field of size 0 at the end => round up the size.  */
  1946.               else
  1947.             round_up_size = EMPTY_FIELD_BOUNDARY;
  1948.             }
  1949.           else
  1950.             {
  1951.               DECL_INITIAL (x) = NULL_TREE;
  1952.               DECL_SIZE_UNIT (x) = width;
  1953.               TREE_PACKED (x) = 1;
  1954.               /* Traditionally a bit field is unsigned
  1955.              even if declared signed.  */
  1956.               if (flag_traditional
  1957.               && TREE_CODE (TREE_TYPE (x)) == INTEGER_TYPE)
  1958.             TREE_TYPE (x) = unsigned_type_node;
  1959.             }
  1960.         }
  1961.           else
  1962.         /* Non-bit-fields are aligned for their type.  */
  1963.         DECL_ALIGN (x) = MAX (DECL_ALIGN (x), TYPE_ALIGN (TREE_TYPE (x)));
  1964.         }
  1965.       else if (TREE_CODE (x) == FIELD_DECL)
  1966.         {
  1967.           tree type = TREE_TYPE (x);
  1968.           if (TREE_CODE (type) == ARRAY_TYPE)
  1969.         type = TREE_TYPE (type);
  1970.           if (code == UNION_TYPE)
  1971.         {
  1972.           if (TYPE_NEEDS_CONSTRUCTING (type))
  1973.             error ("member %s::%s with constructor not allowed in union",
  1974.                IDENTIFIER_POINTER (name), IDENTIFIER_POINTER (DECL_NAME (x)));
  1975.           if (TYPE_NEEDS_DESTRUCTOR (type))
  1976.             error ("member %s::%s with destructor (also) not allowed in union",
  1977.                IDENTIFIER_POINTER (name), IDENTIFIER_POINTER (DECL_NAME (x)));
  1978.         }
  1979.           else if (code == RECORD_TYPE)
  1980.         {
  1981.           /* Array of record type doesn't matter for this bit.  */
  1982.           TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
  1983.           if (IS_AGGR_TYPE (type))
  1984.             {
  1985.               needs_ctor |= TYPE_NEEDS_CONSTRUCTOR (type);
  1986.               needs_dtor |= TYPE_NEEDS_DESTRUCTOR (type);
  1987.               members_need_dtors |= TYPE_NEEDS_DESTRUCTOR (type);
  1988.               TYPE_GETS_ASSIGNMENT (t) |= TYPE_GETS_ASSIGNMENT (type);
  1989.             }
  1990.         }
  1991.           if (DECL_INITIAL (x) != NULL_TREE)
  1992.         {
  1993.           /* `build_class_init_list' does not recognize non-FIELD_DECLs.  */
  1994.           if (code == UNION_TYPE && any_default_members != 0)
  1995.             error ("multiple fields in union initialized");
  1996.           any_default_members = 1;
  1997.         }
  1998.         }
  1999.       y = x;
  2000.     }
  2001.       list_of_fieldlists = TREE_CHAIN (list_of_fieldlists);
  2002.       /* link the tail while we have it! */
  2003.       if (y)
  2004.     {
  2005.       TREE_CHAIN (y) = NULL_TREE;
  2006.  
  2007.       if (list_of_fieldlists
  2008.           && TREE_VALUE (list_of_fieldlists)
  2009.           && TREE_CODE (TREE_VALUE (list_of_fieldlists)) != FUNCTION_DECL)
  2010.         TREE_CHAIN (y) = TREE_VALUE (list_of_fieldlists);
  2011.     }
  2012.     }
  2013.  
  2014.   if (tail) TREE_CHAIN (tail) = NULL_TREE;
  2015.  
  2016.   /* If this type has any constant members which did not come
  2017.      with their own initialization, mark that fact here.  It is
  2018.      not an error here, since such types can be saved either by their
  2019.      constructors, or by fortuitous initialization.  */
  2020.   CLASSTYPE_READONLY_FIELDS_NEED_INIT (t) = const_sans_init;
  2021.   CLASSTYPE_REF_FIELDS_NEED_INIT (t) = ref_sans_init;
  2022.   CLASSTYPE_ABSTRACT_VIRTUALS (t) = abstract_virtuals;
  2023.  
  2024.   if (fields == 0 && n_baseclasses == 0
  2025.       && (pedantic || extra_warnings))
  2026.     warning ((code == UNION_TYPE ? "union has no data members"
  2027.           : "structure has no data members"));
  2028.  
  2029.   if (vfield == 0
  2030.       && (has_virtual
  2031. #ifdef SOS
  2032.       || TYPE_DYNAMIC (t)
  2033. #endif
  2034.       ))
  2035.     {
  2036.       /* We build this decl with ptr_type_node, and
  2037.      change the type when we know what it should be.  */
  2038.       vfield = build_decl (FIELD_DECL, get_vfield_name (t), ptr_type_node);
  2039.       CLASSTYPE_VFIELD (t) = vfield;
  2040.       DECL_FIELD_CONTEXT (vfield) = t;
  2041.       SET_DECL_FCONTEXT (vfield, t);
  2042.       DECL_SIZE_UNIT (vfield) = 0;
  2043.       if (y)
  2044.     {
  2045.       assert (TREE_CHAIN (y) == 0);
  2046.       TREE_CHAIN (y) = vfield;
  2047.       y = vfield;
  2048.     }
  2049.       else fields = vfield;
  2050.       vfields = chainon (vfields, CLASSTYPE_AS_LIST (t));
  2051.     }
  2052.  
  2053.   /* Now DECL_INITIAL is null on all members except for zero-width bit-fields.
  2054.      And they have already done their work.
  2055.  
  2056.      C++: maybe we will support default field initialization some day...  */
  2057.  
  2058.   /* Delete all zero-width bit-fields from the front of the fieldlist */
  2059.   while (fields && TREE_PACKED (fields)
  2060.      && DECL_INITIAL (fields))
  2061.     fields = TREE_CHAIN (fields);
  2062.   /* Delete all such fields from the rest of the fields.  */
  2063.   for (x = fields; x;)
  2064.     {
  2065.       if (TREE_CHAIN (x) && TREE_PACKED (TREE_CHAIN (x))
  2066.       && DECL_INITIAL (TREE_CHAIN (x)))
  2067.     TREE_CHAIN (x) = TREE_CHAIN (TREE_CHAIN (x));
  2068.       else x = TREE_CHAIN (x);
  2069.     }
  2070.   /* Delete all duplicate fields from the fields */
  2071.   delete_duplicate_fields (fields);
  2072.  
  2073.   /* Now we have the final fieldlist for the data fields.  Record it,
  2074.      then lay out the structure or union (including the fields).  */
  2075.  
  2076.   TYPE_FIELDS (t) = fields;
  2077.  
  2078.   /* If there's a :0 field at the end, round the size to the
  2079.      EMPTY_FIELD_BOUNDARY.  */
  2080.   TYPE_ALIGN (t) = round_up_size;
  2081.  
  2082.   if (debug_default_functions)
  2083.     {
  2084.       if ((TYPE_NEEDS_CONSTRUCTOR (t) || TYPE_HAS_CONSTRUCTOR (t) || needs_ctor)
  2085.       && ! TYPE_HAS_INIT_REF (t))
  2086.     {
  2087.       tree default_fn = cons_up_default_function (t, name, 1);
  2088.       TREE_CHAIN (default_fn) = fn_fields;
  2089.       DECL_CONTEXT (default_fn) = t;
  2090.       DECL_VCONTEXT (default_fn) = t;
  2091.       fn_fields = default_fn;
  2092.       TYPE_HAS_INIT_REF (t) = 1;
  2093.       default_fn = cons_up_default_function (t, name, 3);
  2094.       TREE_CHAIN (default_fn) = fn_fields;
  2095.       DECL_CONTEXT (default_fn) = t;
  2096.       DECL_VCONTEXT (default_fn) = t;
  2097.       fn_fields = default_fn;
  2098.       nonprivate_method = 1;
  2099.     }
  2100.  
  2101.       if (! TYPE_HAS_DEFAULT_CONSTRUCTOR (t)
  2102.       && needs_default_ctor && ! cant_have_default_ctor)
  2103.     {
  2104.       tree default_fn = cons_up_default_function (t, name, 2);
  2105.       TREE_CHAIN (default_fn) = fn_fields;
  2106.       DECL_CONTEXT (default_fn) = t;
  2107.       DECL_VCONTEXT (default_fn) = t;
  2108.       fn_fields = default_fn;
  2109.       TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 1;
  2110.       nonprivate_method = 1;
  2111.     }
  2112.     }
  2113.   /* Warn about duplicate methods in fn_fields.  Also compact
  2114.      method lists so that lookup can be made faster.
  2115.  
  2116.      Algorithm:  Outer loop builds lists by method name.
  2117.      Inner loop checks for redundant method names within a list.
  2118.  
  2119.      Data Structure:  List of method lists.  The outer list
  2120.      is a TREE_LIST, whose TREE_PURPOSE field is the field name
  2121.      and the TREE_VALUE is the TREE_CHAIN of the FUNCTION_DECLs.
  2122.      Friends are chained in the same way as member functions, but
  2123.      they live in the TREE_TYPE field of the outer list.
  2124.      That allows them to be quicky deleted, and requires
  2125.      no extra storage.
  2126.  
  2127.      If there are any constructors/destructors, they are moved to
  2128.      the front of the list.  This makes pushclass more efficient.
  2129.  
  2130.      We also link each field which has shares a name with its
  2131.      baseclass to the head of the list of fields for that base class.
  2132.      This allows us to reduce search time in places like `build_method_call'
  2133.      to consider only reasonably likely functions.  */
  2134.  
  2135.   if (fn_fields)
  2136.     {
  2137.       /* Now prepare to gather fn_fields into vector.  */
  2138.       struct obstack *ambient_obstack = current_obstack;
  2139.       current_obstack = &class_obstack;
  2140.       method_vec = make_node (TREE_VEC);
  2141.       /* Room has been saved for constructors and destructors.  */
  2142.       current_obstack = ambient_obstack;
  2143.       /* Now make this a live vector.  */
  2144.       obstack_free (&class_obstack, method_vec);
  2145.       obstack_blank (&class_obstack, sizeof (struct tree_vec));
  2146.  
  2147.       while (fn_fields)
  2148.     {
  2149.       /* NEXT Pointer, TEST Pointer, and BASE Pointer.  */
  2150.       tree nextp, *testp;
  2151.  
  2152.       nextp = TREE_CHAIN (fn_fields);
  2153.       TREE_CHAIN (fn_fields) = NULL_TREE;
  2154.       /* Constrcutors are handled easily in search routines.
  2155.          Besides, we know we wont find any, so do not bother looking.  */
  2156.       if (DECL_ORIGINAL_NAME (fn_fields) == name
  2157.           && TREE_VEC_ELT (method_vec, 0) == 0)
  2158.         TREE_VEC_ELT (method_vec, 0) = fn_fields;
  2159.       else
  2160.         {
  2161.           testp = &TREE_VEC_ELT (method_vec, 0);
  2162.           if (*testp == NULL_TREE)
  2163.         testp++;
  2164.           while ((int)testp < (int)obstack_next_free (&class_obstack)
  2165.              && DECL_ORIGINAL_NAME (*testp) != DECL_ORIGINAL_NAME (fn_fields))
  2166.         testp++;
  2167.           if ((int)testp < (int)obstack_next_free (&class_obstack))
  2168.         {
  2169.           for (x = *testp; x; x = TREE_CHAIN (x))
  2170.             {
  2171.               if (DECL_NAME (fn_fields) == DECL_NAME (x))
  2172.             {
  2173.               /* We complain about multiple destructors on sight,
  2174.                  so we do not repeat the warning here.  Friend-friend
  2175.                  ambiguities are warned about outside this loop.  */
  2176.               if (! DESTRUCTOR_NAME_P (DECL_NAME (fn_fields)))
  2177.                 error_with_file_and_line (DECL_SOURCE_FILE (fn_fields),
  2178.                               DECL_SOURCE_LINE (fn_fields),
  2179.                               "ambiguous method `%s' in structure",
  2180.                               lang_printable_name (fn_fields));
  2181.               break;
  2182.             }
  2183.               y = x;
  2184.             }
  2185.           if (x == 0)
  2186.             if (*testp)
  2187.               TREE_CHAIN (y) = fn_fields;
  2188.             else
  2189.               *testp = fn_fields;
  2190.         }
  2191.           else
  2192.         {
  2193.           obstack_ptr_grow (&class_obstack, fn_fields);
  2194.           method_vec = (tree)obstack_base (&class_obstack);
  2195.         }
  2196.         }
  2197.       fn_fields = nextp;
  2198.     }
  2199.  
  2200.       TREE_VEC_LENGTH (method_vec)
  2201.     = (tree *)obstack_next_free (&class_obstack) - (&TREE_VEC_ELT (method_vec, 0));
  2202.       obstack_finish (&class_obstack);
  2203.       CLASSTYPE_METHOD_VEC (t) = method_vec;
  2204.  
  2205.       if (nonprivate_method == 0
  2206.       && CLASSTYPE_FRIEND_CLASSES (t) == NULL_TREE
  2207.       && DECL_FRIENDLIST (TYPE_NAME (t)) == NULL_TREE)
  2208.     {
  2209.       for (i = 0; i <= n_baseclasses; i++)
  2210.         if (CLASSTYPE_VIA_PUBLIC (t, i))
  2211.           {
  2212.         nonprivate_method = 1;
  2213.         break;
  2214.           }
  2215.       if (nonprivate_method == 0)
  2216.         warning ("all class member functions are private");
  2217.     }
  2218.     }
  2219.   else
  2220.     {
  2221.       method_vec = 0;
  2222.  
  2223.       /* Just in case these got accidently
  2224.      filled in by syntax errors.  */
  2225.       TYPE_HAS_CONSTRUCTOR (t) = 0;
  2226.       TYPE_HAS_DESTRUCTOR (t) = 0;
  2227.     }
  2228.  
  2229.   /* If there are constructors (and destructors), they are at the
  2230.      front.  Place destructors at very front.  Also warn if all
  2231.      constructors and/or destructors are private (in which case this
  2232.      class is effectively unusable.  */
  2233.   if (TYPE_HAS_DESTRUCTOR (t))
  2234.     {
  2235.       tree dtor, prev;
  2236.  
  2237.       for (dtor = TREE_VEC_ELT (method_vec, 0); dtor; prev = dtor, dtor = TREE_CHAIN (dtor))
  2238.     {
  2239.       if (DESTRUCTOR_NAME_P (DECL_NAME (dtor)))
  2240.         {
  2241.           if (TREE_PRIVATE (dtor)
  2242.           && CLASSTYPE_FRIEND_CLASSES (t) == NULL_TREE
  2243.           && DECL_FRIENDLIST (TYPE_NAME (t)) == NULL_TREE)
  2244.         warning_with_decl (TYPE_NAME (t), "class `%s' only defines a private destructor and has no friends");
  2245.           break;
  2246.         }
  2247.     }
  2248.       /* Wild parse errors can cause this to happen.  */
  2249.       if (dtor == NULL_TREE)
  2250.     TYPE_HAS_DESTRUCTOR (t) = 0;
  2251.       else if (dtor != TREE_VEC_ELT (method_vec, 0))
  2252.     {
  2253.       TREE_CHAIN (prev) = TREE_CHAIN (dtor);
  2254.       TREE_CHAIN (dtor) = TREE_VEC_ELT (method_vec, 0);
  2255.       TREE_VEC_ELT (method_vec, 0) = dtor;
  2256.     }
  2257.     }
  2258.   else if (members_need_dtors
  2259.        || (TYPE_USES_VIRTUAL_BASECLASSES (t)
  2260.            || TYPE_USES_MULTIPLE_INHERITANCE (t)))
  2261.     {
  2262.       /* Here we must cons up a destructor on the fly.  */
  2263.       tree dtor = cons_up_default_function (t, name, 0);
  2264.  
  2265.       /* If we couldn't make it work, then pretend we didn't need it.  */
  2266.       if (dtor == void_type_node)
  2267.     TYPE_NEEDS_DESTRUCTOR (t) = 0;
  2268.       else
  2269.     {
  2270.       DECL_CONTEXT (dtor) = t;
  2271.       DECL_VCONTEXT (dtor) = t;
  2272.       if (DECL_VIRTUAL_P (dtor))
  2273.         pending_virtuals = add_virtual_function (pending_virtuals,
  2274.                              &has_virtual, dtor);
  2275.       if (TYPE_HAS_CONSTRUCTOR (t))
  2276.         TREE_CHAIN (dtor) = TREE_VEC_ELT (method_vec, 0);
  2277.       else if (method_vec == 0)
  2278.         {
  2279.           /* Now prepare to gather fn_fields into vector.  */
  2280.           struct obstack *ambient_obstack = current_obstack;
  2281.           current_obstack = &class_obstack;
  2282.           method_vec = make_node (TREE_VEC);
  2283.           /* Room has been saved for constructors and destructors.  */
  2284.           current_obstack = ambient_obstack;
  2285.           TREE_VEC_LENGTH (method_vec) = 1;
  2286.           CLASSTYPE_METHOD_VEC (t) = method_vec;
  2287.         }
  2288.       TREE_VEC_ELT (method_vec, 0) = dtor;
  2289.       TYPE_HAS_DESTRUCTOR (t) = 1;
  2290.     }
  2291.     }
  2292.   if (TYPE_HAS_CONSTRUCTOR (t)
  2293.       && ! CLASSTYPE_DECLARED_EXCEPTION (t)
  2294.       && CLASSTYPE_FRIEND_CLASSES (t) == NULL_TREE
  2295.       && DECL_FRIENDLIST (TYPE_NAME (t)) == NULL_TREE)
  2296.     {
  2297.       int nonprivate_ctor = 0;
  2298.       tree ctor;
  2299.  
  2300.       for (ctor = TREE_VEC_ELT (method_vec, 0); ctor; ctor = TREE_CHAIN (ctor))
  2301.     if (! TREE_PRIVATE (ctor))
  2302.       {
  2303.         nonprivate_ctor = 1;
  2304.         break;
  2305.       }
  2306.       if (nonprivate_ctor == 0)
  2307.     warning ("class %s only defines private constructors and has no friends",
  2308.          TYPE_NAME_STRING (t));
  2309.     }
  2310.  
  2311.   /* Now for each member function (except for constructors and
  2312.      destructors), compute where member functions of the same
  2313.      name reside in base classes.  */
  2314.   if (n_baseclasses != 0
  2315.       && method_vec != NULL_TREE
  2316.       && TREE_VEC_LENGTH (method_vec) > 1)
  2317.     {
  2318.       int len = TREE_VEC_LENGTH (method_vec);
  2319.       tree baselink_vec = make_tree_vec (len);
  2320.       int any_links = 0;
  2321.  
  2322.       for (i = 1; i < len; i++)
  2323.     {
  2324.       TREE_VEC_ELT (baselink_vec, i)
  2325.         = get_baselinks (t, DECL_ORIGINAL_NAME (TREE_VEC_ELT (method_vec, i)));
  2326.       if (TREE_VEC_ELT (baselink_vec, i) != 0)
  2327.         any_links = 1;
  2328.     }
  2329.       if (any_links != 0)
  2330.     CLASSTYPE_BASELINK_VEC (t) = baselink_vec;
  2331.       else
  2332.     obstack_free (current_obstack, baselink_vec);
  2333.     }
  2334.  
  2335.   /* We can't know this information until we have seen all of the
  2336.      constructors.  */
  2337.   TYPE_NONE_ASSIGN_THIS (t) = 0;
  2338.  
  2339.   /* Pass layout information about base classes to layout_type, if any.  */
  2340.  
  2341.   if (n_baseclasses)
  2342.     {
  2343.       tree pseudo_basetype = TREE_TYPE (base_layout_decl);
  2344.  
  2345.       TREE_CHAIN (base_layout_decl) = TYPE_FIELDS (t);
  2346.       TYPE_FIELDS (t) = base_layout_decl;
  2347.  
  2348.       TYPE_SIZE (pseudo_basetype) = CLASSTYPE_SIZE (t);
  2349.       TYPE_SIZE_UNIT (pseudo_basetype) = TYPE_SIZE_UNIT (t);
  2350.       TYPE_MODE (pseudo_basetype) = TYPE_MODE (t);
  2351.       TYPE_ALIGN (pseudo_basetype) = CLASSTYPE_ALIGN (t);
  2352.       DECL_ALIGN (base_layout_decl) = TYPE_ALIGN (pseudo_basetype);
  2353.     }
  2354.  
  2355.   layout_type (t);
  2356.  
  2357.   if (n_baseclasses)
  2358.     TYPE_FIELDS (t) = TREE_CHAIN (TYPE_FIELDS (t));
  2359.  
  2360.   /* C++: do not let empty structures exist.  */
  2361.   if (integer_zerop (TYPE_SIZE (t)))
  2362.     TYPE_SIZE (t) = TYPE_SIZE (char_type_node);
  2363.  
  2364.   /* Set the TYPE_DECL for this type to contain the right
  2365.      value for DECL_OFFSET, so that we can use it as part
  2366.      of a COMPONENT_REF for multiple inheritance.  */
  2367.  
  2368.   if (TREE_CODE (TYPE_NAME (t)) == TYPE_DECL)
  2369.     layout_decl (TYPE_NAME (t));
  2370.  
  2371.   if (TYPE_USES_VIRTUAL_BASECLASSES (t))
  2372.     {
  2373.       tree vbases;
  2374.  
  2375.       max_has_virtual = layout_vbasetypes (t, max_has_virtual);
  2376.       vbases = CLASSTYPE_VBASECLASSES (t);
  2377.       CLASSTYPE_N_VBASECLASSES (t) = list_length (vbases);
  2378.  
  2379.       /* Now fix up any virtual base class types that we
  2380.      left lying around.  We must get these done
  2381.      before we try to lay out the virtual function table.  */
  2382.       pending_hard_virtuals = nreverse (pending_hard_virtuals);
  2383. #if 1
  2384.       /* This loop makes all the entries in the virtual function tables
  2385.      of interest contain the "latest" version of the functions
  2386.      we have defined.  */
  2387.  
  2388.       while (vbases)
  2389.     {
  2390.       tree virtuals = ASSOC_VIRTUALS (vbases);
  2391.  
  2392.       if (virtuals)
  2393.         virtuals = TREE_CHAIN (virtuals);
  2394.  
  2395.       while (virtuals != NULL_TREE)
  2396.         {
  2397.           tree pfn = FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (virtuals));
  2398.           tree base_fndecl = TREE_OPERAND (pfn, 0);
  2399.           tree decl = get_first_matching_virtual (t, base_fndecl, 0);
  2400.           tree context = DECL_CONTEXT (decl);
  2401.           if (decl != base_fndecl && context != t)
  2402.         {
  2403.           tree assoc = NULL_TREE, these_virtuals;
  2404.           int i = TREE_INT_CST_LOW (DECL_VINDEX (base_fndecl)) & ((1<<(BITS_PER_WORD-1))-1);
  2405.  
  2406.           if (TYPE_USES_VIRTUAL_BASECLASSES (context))
  2407.             assoc = value_member (DECL_CONTEXT (base_fndecl),
  2408.                       CLASSTYPE_VBASECLASSES (context));
  2409.           if (assoc == NULL_TREE)
  2410.             assoc = assoc_value (DECL_CONTEXT (base_fndecl), context);
  2411.           if (assoc != NULL_TREE)
  2412.             {
  2413.               these_virtuals = ASSOC_VIRTUALS (assoc);
  2414.  
  2415.               while (i-- > 0)
  2416.             these_virtuals = TREE_CHAIN (these_virtuals);
  2417.               pfn = FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (these_virtuals));
  2418.               modify_vtable_entries (t, decl, base_fndecl, pfn);
  2419.             }
  2420.         }
  2421.           virtuals = TREE_CHAIN (virtuals);
  2422.         }
  2423.       vbases = TREE_CHAIN (vbases);
  2424.     }
  2425. #endif /* 1 */
  2426.       while (pending_hard_virtuals)
  2427.     {
  2428.       /* Need an entry in some other virtual function table.  */
  2429.       tree base_fndecls = DECL_VINDEX (TREE_PURPOSE (pending_hard_virtuals));
  2430.       while (base_fndecls)
  2431.         {
  2432.           modify_vtable_entries (t, TREE_PURPOSE (pending_hard_virtuals),
  2433.                      TREE_VALUE (base_fndecls),
  2434.                      TREE_VALUE (pending_hard_virtuals));
  2435.           base_fndecls = TREE_CHAIN (base_fndecls);
  2436.         }
  2437.       pending_hard_virtuals = TREE_CHAIN (pending_hard_virtuals);
  2438.     }
  2439.     }
  2440.   else
  2441.     CLASSTYPE_VBASE_SIZE (t) = integer_zero_node;
  2442.  
  2443.   if (pending_virtuals)
  2444.     {
  2445.       pending_virtuals = nreverse (pending_virtuals);
  2446.       /* We must enter these virtuals into the table.  */
  2447.       if (first_vfn_base_index == 0)
  2448.     {
  2449.       pending_virtuals = tree_cons (NULL_TREE, the_null_vtable_entry,
  2450.                     pending_virtuals);
  2451.       build_vtable (0, t);
  2452.     }
  2453.       else
  2454.     {
  2455.       /* Here we know enough to change the type of our virtual
  2456.          function table, but we will wait until later this function.  */
  2457.       if (! CLASSTYPE_MARKED4 (t))
  2458.         build_vtable (assoc_value (TYPE_MAIN_VARIANT (CLASSTYPE_BASECLASS (t, first_vfn_base_index)), t), t);
  2459.     }
  2460.  
  2461.       /* If this type has basetypes with constructors, then those
  2462.      constructors might clobber the virtual function table.  But
  2463.      they don't if the derived class shares the exact vtable of the base
  2464.      class.  */
  2465.  
  2466.       CLASSTYPE_NEEDS_VIRTUAL_REINIT (t) = 1;
  2467.     }
  2468.   else if (first_vfn_base_index)
  2469.     {
  2470.       tree basetype = get_base_type (DECL_FIELD_CONTEXT (vfield), t, 0);
  2471.       tree assoc;
  2472.       
  2473.       if (TREE_VIA_VIRTUAL (basetype))
  2474.     assoc = value_member (DECL_FIELD_CONTEXT (vfield), CLASSTYPE_VBASECLASSES (t));
  2475.       else
  2476.     assoc = assoc_value (TYPE_MAIN_VARIANT (basetype), t);
  2477.  
  2478.       /* This class contributes nothing new to the virtual function
  2479.      table.  However, it may have declared functions which
  2480.      went into the virtual function table "inherited" from the
  2481.      base class.  If so, we grab a copy of those updated functions,
  2482.      and pretend they are ours.  */
  2483.  
  2484. #ifdef SOS
  2485.       /* Don't define this ahead of time if we have more
  2486.      fields to add later.  */
  2487.       if (all_virtual == 2 && fn_fields != NULL_TREE)
  2488.     ;
  2489.       else
  2490. #endif
  2491.     {
  2492.       /* See if we should steal the virtual info from base class.  */
  2493.       if (CLASS_ASSOC_VTABLE (t) == NULL_TREE)
  2494.         CLASS_ASSOC_VTABLE (t) = ASSOC_VTABLE (assoc);
  2495.       if (CLASS_ASSOC_VIRTUALS (t) == NULL_TREE)
  2496.         CLASS_ASSOC_VIRTUALS (t) = ASSOC_VIRTUALS (assoc);
  2497.     }
  2498.       if (CLASS_ASSOC_VTABLE (t) != ASSOC_VTABLE (assoc))
  2499.     CLASSTYPE_NEEDS_VIRTUAL_REINIT (t) = 1;
  2500.     }
  2501.  
  2502.   if (has_virtual > max_has_virtual)
  2503.     max_has_virtual = has_virtual;
  2504.   if (max_has_virtual || first_vfn_base_index)
  2505.     {
  2506. #ifdef VTABLE_USES_MASK
  2507.       if (max_has_virtual >= VINDEX_MAX)
  2508.     {
  2509.       error ("too many virtual functions for class `%s' (VINDEX_MAX < %d)", TYPE_NAME_STRING (t), has_virtual);
  2510.     }
  2511. #endif
  2512.       TYPE_VIRTUAL_P (t) = 1;
  2513.       CLASSTYPE_VSIZE (t) = has_virtual;
  2514.       if (first_vfn_base_index)
  2515.     {
  2516.       if (pending_virtuals)
  2517.         CLASS_ASSOC_VIRTUALS (t) = chainon (CLASS_ASSOC_VIRTUALS (t),
  2518.                         pending_virtuals);
  2519.     }
  2520.       else if (has_virtual)
  2521.     {
  2522.       CLASS_ASSOC_VIRTUALS (t) = pending_virtuals;
  2523.       if (write_virtuals >= 0)
  2524.         DECL_VIRTUAL_P (CLASS_ASSOC_VTABLE (t)) = 1;
  2525.     }
  2526.     }
  2527.  
  2528. #ifdef SOS
  2529.   if (all_virtual == 2 && (max_has_virtual || method_vec))
  2530.     {
  2531.       /* Now that we know the size of the virtual table, lay out
  2532.      the absolute table following it.  */
  2533.       int i;
  2534.       tree tmp;
  2535.       tree pending_absolutes = NULL_TREE;
  2536.       int has_absolute = has_virtual;
  2537.  
  2538.       /* Local variables for building a table filled with strings
  2539.      containing the names of interesting things.  */
  2540.       tree decl, init;
  2541.       tree start = NULL_TREE, next = NULL_TREE;
  2542.       tree *outer = &TREE_VEC_ELT (method_vec, 0);
  2543.  
  2544.       while (outer != TREE_VEC_END (method_vec))
  2545.     {
  2546.       tree inner;
  2547.       for (inner = *outer; inner; inner = TREE_CHAIN (inner))
  2548.         {
  2549.           tree entry;
  2550.           tree fn;
  2551.  
  2552.           /* Don't bother with functions which appear
  2553.          for visibility reasons.  */
  2554.           if (DECL_FIELD_CONTEXT (inner) != t)
  2555.         continue;
  2556.  
  2557.           /* Must lay this function into its absolute table as well.
  2558.          This forces an inline function to be written out.  */
  2559.           fn = build1 (ADDR_EXPR, ptr_type_node, inner);
  2560.           TREE_LITERAL (fn) = 1;
  2561.           DECL_DINDEX (inner) = build_int_2 (++has_absolute, 0);
  2562.           entry = build_vtable_entry (integer_zero_node, fn);
  2563.           pending_absolutes = tree_cons (DECL_DINDEX (inner), entry,
  2564.                          pending_absolutes);
  2565.         }
  2566.       outer++;
  2567.     }
  2568.  
  2569.       CLASS_ASSOC_VIRTUALS (t) = chainon (CLASS_ASSOC_VIRTUALS (t),
  2570.                       nreverse (pending_absolutes));
  2571.       if (TYPE_DYNAMIC (t))
  2572.     {
  2573.       for (outer = &TREE_VEC_ELT (method_vec, 0);
  2574.            outer != TREE_VEC_END (method_vec);
  2575.            outer++)
  2576.         {
  2577.           tree inner;
  2578.           for (inner = *outer; inner; inner = TREE_CHAIN (inner))
  2579.         {
  2580.           tree str = make_node (STRING_CST);
  2581.           TREE_STRING_LENGTH (str) = IDENTIFIER_LENGTH (DECL_NAME (inner));
  2582.           TREE_STRING_POINTER (str) = IDENTIFIER_POINTER (DECL_NAME (inner));
  2583.           TREE_LITERAL (str) = 1;
  2584.           TREE_STATIC (str) = 1;
  2585.           TREE_TYPE (str)
  2586.             = build_cplus_array_type (char_type_node,
  2587.                           build_index_type (build_int_2 (TREE_STRING_LENGTH (str) - 1, 0)));
  2588.           
  2589.           if (start)
  2590.             {
  2591.               TREE_CHAIN (next) = build_tree_list (NULL_TREE, str);
  2592.               next = TREE_CHAIN (next);
  2593.             }
  2594.           else
  2595.             {
  2596.               start = build_tree_list (NULL_TREE, str);
  2597.               next = start;
  2598.             }
  2599.         }
  2600.         }
  2601.  
  2602.       /* Lay out dynamic link table for SOS.  */
  2603.  
  2604.       decl = finish_table (get_linktable_name (t),
  2605.                    string_type_node, start, 0);
  2606.     }
  2607.       has_virtual = has_absolute;
  2608.       CLASSTYPE_VSIZE (t) = has_virtual;
  2609.       if (has_virtual > max_has_virtual)
  2610.     max_has_virtual = has_virtual;
  2611.       if (vfield == 0)
  2612.     {
  2613.       /* We build this decl with ptr_type_node, and
  2614.          change the type when we know what it should be.  */
  2615.       vfield = build_decl (FIELD_DECL, get_vfield_name (t), ptr_type_node);
  2616.       CLASSTYPE_VFIELD (t) = vfield;
  2617.       DECL_FIELD_CONTEXT (vfield) = t;
  2618.       SET_DECL_FCONTEXT (vfield, t);
  2619.       DECL_SIZE_UNIT (vfield) = 0;
  2620.       y = tree_last (fields);
  2621.       if (y)
  2622.         TREE_CHAIN (y) = vfield;
  2623.       else
  2624.         fields = vfield;
  2625.       vfields = chainon (vfields, CLASSTYPE_AS_LIST (t));
  2626.     }
  2627.     }
  2628. #endif
  2629.  
  2630.   /* Now lay out the virtual function table.  */
  2631.   if (has_virtual)
  2632.     {
  2633.       tree atype, itype;
  2634.  
  2635.       if (TREE_TYPE (vfield) == ptr_type_node)
  2636.     {
  2637.       /* We must create a pointer to this table because
  2638.          the one inherited from base class does not exist.
  2639.          We will fill in the type when we know what it
  2640.          should really be.  */
  2641.       itype = build_index_type (build_int_2 (has_virtual, 0));
  2642.       atype = build_array_type (vtable_entry_type, itype);
  2643.       layout_type (atype);
  2644.       TREE_TYPE (vfield) = build_pointer_type (atype);
  2645.     }
  2646.       else
  2647.     {
  2648.       atype = TREE_TYPE (TREE_TYPE (vfield));
  2649.  
  2650.       if (has_virtual != TREE_INT_CST_LOW (TYPE_MAX_VALUE (TYPE_DOMAIN (atype))))
  2651.         {
  2652.           /* We must extend (or create) the boundaries on this array,
  2653.          because we picked up virtual functions from multiple
  2654.          base classes.  */
  2655.           itype = build_index_type (build_int_2 (has_virtual, 0));
  2656.           atype = build_array_type (vtable_entry_type, itype);
  2657.           layout_type (atype);
  2658.           vfield = copy_node (vfield);
  2659.           TREE_TYPE (vfield) = build_pointer_type (atype);
  2660. #if 0
  2661.           /* In the case of single inheritance, we can
  2662.          just move up the tree, since we share the
  2663.          same vptr slot.  */
  2664.           if (TREE_CHAIN (vfields) == NULL_TREE)
  2665.         vfields = CLASSTYPE_AS_LIST (t);
  2666. #endif
  2667.         }
  2668.     }
  2669.  
  2670.       CLASSTYPE_VFIELD (t) = vfield;
  2671.       if (TREE_TYPE (CLASS_ASSOC_VTABLE (t)) != atype)
  2672.     {
  2673.       TREE_TYPE (CLASS_ASSOC_VTABLE (t)) = atype;
  2674.       layout_decl (CLASS_ASSOC_VTABLE (t));
  2675.       DECL_ALIGN (CLASS_ASSOC_VTABLE (t))
  2676.         = MAX (TYPE_ALIGN (double_type_node),
  2677.            DECL_ALIGN (CLASS_ASSOC_VTABLE (t)));
  2678.     }
  2679.     }
  2680.   else if (first_vfn_base_index)
  2681.     CLASSTYPE_VFIELD (t) = vfield;
  2682.   CLASSTYPE_VFIELDS (t) = vfields;
  2683.  
  2684.   /* Set all appropriate CLASSTYPE_... flags for this type
  2685.      and its variants.  */
  2686.   TYPE_NEEDS_CONSTRUCTOR (t) |= needs_ctor || TYPE_HAS_CONSTRUCTOR (t);
  2687.   TYPE_NEEDS_CONSTRUCTING (t)
  2688.     |= ((TYPE_NEEDS_CONSTRUCTOR (t)|TYPE_USES_VIRTUAL_BASECLASSES (t))
  2689.     || (has_virtual | first_vfn_base_index)
  2690.     || any_default_members);
  2691.   TYPE_NEEDS_DESTRUCTOR (t) |= needs_dtor || TYPE_HAS_DESTRUCTOR (t);
  2692.   finish_struct_bits (t, first_vfn_base_index, max_has_virtual);
  2693.  
  2694.   /* Promote each bit-field's type to int if it is narrower than that.
  2695.      Also warn (or error) if static members are specified for a class
  2696.      which takes a constructor.  */
  2697.   for (x = fields; x; x = TREE_CHAIN (x))
  2698.     {
  2699.       if (TREE_PACKED (x)
  2700.       && TREE_CODE (TREE_TYPE (x)) == INTEGER_TYPE
  2701.       && (TREE_INT_CST_LOW (DECL_SIZE (x)) * DECL_SIZE_UNIT (x)
  2702.           < TYPE_PRECISION (integer_type_node)))
  2703.     TREE_TYPE (x) = integer_type_node;
  2704.     }
  2705.  
  2706.   if ((! TYPE_HAS_CONSTRUCTOR (t) && TYPE_NEEDS_CONSTRUCTING (t))
  2707.       || any_default_members != 0)
  2708.     build_class_init_list (t);
  2709.  
  2710.   if (current_lang_name == lang_name_cplusplus)
  2711.     {
  2712.       if (! CLASSTYPE_DECLARED_EXCEPTION (t))
  2713.     embrace_waiting_friends (t);
  2714.  
  2715.       /* Write out inline function definitions.  */
  2716.       do_inline_function_hair (t, CLASSTYPE_INLINE_FRIENDS (t));
  2717.       CLASSTYPE_INLINE_FRIENDS (t) = 0;
  2718.     }
  2719.  
  2720.   if (CLASSTYPE_VSIZE (t) != 0)
  2721.     {
  2722.       TYPE_NONCOPIED_PARTS (t) = build_tree_list (default_conversion (CLASS_ASSOC_VTABLE (t)), vfield);
  2723.  
  2724.       if (! flag_this_is_variable)
  2725.     {
  2726.       tree vtbl_ptr = build_decl (VAR_DECL, get_identifier (VPTR_NAME),
  2727.                       TREE_TYPE (vfield));
  2728.       TREE_REGDECL (vtbl_ptr) = 1;
  2729.       CLASSTYPE_VTBL_PTR (t) = vtbl_ptr;
  2730.     }
  2731.       if (DECL_FIELD_CONTEXT (vfield) != t)
  2732.     {
  2733.       tree assoc = assoc_value (DECL_FIELD_CONTEXT (vfield), t);
  2734.       tree offset = ASSOC_OFFSET (assoc);
  2735.  
  2736.       vfield = copy_node (vfield);
  2737.  
  2738.       if (! integer_zerop (offset))
  2739.         offset = convert_units (offset, BITS_PER_UNIT, 1);
  2740.       if (DECL_OFFSET (vfield))
  2741.         offset = genop (PLUS_EXPR, offset, build_int (DECL_OFFSET (vfield)));
  2742.       DECL_FIELD_CONTEXT (vfield) = t;
  2743.       DECL_OFFSET (vfield) = TREE_INT_CST_LOW (offset);
  2744.       CLASSTYPE_VFIELD (t) = vfield;
  2745.     }
  2746.     }
  2747.  
  2748.   /* Make the rtl for any new vtables we have created, and unmark
  2749.      the base types we marked.  */
  2750.   unmark_finished_struct (t);
  2751.  
  2752.   /* Now out of this class's scope.  However, if this class defined
  2753.      any new typedefs, then we must export those to the outer
  2754.      binding level.  This is unpleasant.  */
  2755.   x = gettags ();
  2756.  
  2757.   popclass (0);
  2758.  
  2759. #if 0
  2760.   /* Remove aggregate types from the list of tags,
  2761.      since these appear at global scope.  */
  2762.   while (x && IS_AGGR_TYPE (TREE_VALUE (x)))
  2763.     x = TREE_CHAIN (x);
  2764.   CLASSTYPE_TAGS (t) = x;
  2765.   y = x;
  2766.   while (x)
  2767.     {
  2768.       if (IS_AGGR_TYPE (TREE_VALUE (x)))
  2769.     TREE_CHAIN (y) = TREE_CHAIN (x);
  2770.       x = TREE_CHAIN (x);
  2771.     }
  2772. #endif
  2773.  
  2774.   hack_incomplete_structures (t);
  2775.  
  2776.   resume_momentary (old);
  2777.  
  2778.   return t;
  2779. }
  2780.  
  2781. /* Return non-zero if the effective type of INSTANCE is static.
  2782.    Used to determine whether the virtual function table is needed
  2783.    or not.  */
  2784. int
  2785. resolves_to_fixed_type_p (instance)
  2786.      tree instance;
  2787. {
  2788.   switch (TREE_CODE (instance))
  2789.     {
  2790.     case ADDR_EXPR:
  2791.       return resolves_to_fixed_type_p (TREE_OPERAND (instance, 0));
  2792.  
  2793.     case COMPONENT_REF:
  2794.       /* Don't let pointers to members look like they hold a fixed type.  */
  2795.       if (TREE_CODE (TREE_OPERAND (instance, 1)) != FIELD_DECL)
  2796.     return 0;
  2797.  
  2798.     case VAR_DECL:
  2799.     case PARM_DECL:
  2800.     case NEW_EXPR:
  2801.       if (IS_AGGR_TYPE (TREE_TYPE (instance)))
  2802.     return 1;
  2803.  
  2804.     default:
  2805.       return 0;
  2806.     }
  2807. }
  2808.  
  2809. /* Ordering function for overload resolution.  */
  2810. int
  2811. rank_for_overload (x, y)
  2812.      struct candidate *x, *y;
  2813. {
  2814.   if (y->evil - x->evil)
  2815.     return y->evil - x->evil;
  2816.   if ((y->harshness[0] & 128) ^ (x->harshness[0] & 128))
  2817.     return y->harshness[0] - x->harshness[0];
  2818.   if (y->user - x->user)
  2819.     return y->user - x->user;
  2820.   if (y->b_or_d - x->b_or_d)
  2821.     return y->b_or_d - x->b_or_d;
  2822.   return y->easy - x->easy;
  2823. }
  2824.  
  2825. /* TYPE is the type we wish to convert to.  PARM is the parameter
  2826.    we have to work with.  We use a somewhat arbitrary cost function
  2827.    to measure this conversion.  */
  2828. static int
  2829. convert_harshness (type, parmtype, parm)
  2830.      register tree type, parmtype;
  2831.      tree parm;
  2832. {
  2833.   register enum tree_code codel = TREE_CODE (type);
  2834.   register enum tree_code coder = TREE_CODE (parmtype);
  2835.  
  2836. #ifdef GATHER_STATISTICS
  2837.   n_convert_harshness++;
  2838. #endif
  2839.  
  2840.   if (TYPE_MAIN_VARIANT (parmtype) == TYPE_MAIN_VARIANT (type))
  2841.     return 0;
  2842.  
  2843.   if (coder == ERROR_MARK)
  2844.     return 1;
  2845.  
  2846.   if (codel == POINTER_TYPE
  2847.       && (coder == METHOD_TYPE || coder == FUNCTION_TYPE))
  2848.     {
  2849.       tree p1, p2;
  2850.       int harshness, new_harshness;
  2851.  
  2852.       /* Get to the METHOD_TYPE or FUNCTION_TYPE that this might be.  */
  2853.       type = TREE_TYPE (type);
  2854.  
  2855.       if (coder != TREE_CODE (type))
  2856.     return 1;
  2857.  
  2858.       harshness = 0;
  2859.  
  2860.       /* We allow the default conversion between function type
  2861.      and pointer-to-function type for free.  */
  2862.       if (type == parmtype)
  2863.     return 0;
  2864.  
  2865.       /* Compare return types.  */
  2866.       harshness |= convert_harshness (TREE_TYPE (type), TREE_TYPE (parmtype), 0);
  2867.       if (harshness & 1)
  2868.     return 1;
  2869.       p1 = TYPE_ARG_TYPES (type);
  2870.       p2 = TYPE_ARG_TYPES (parmtype);
  2871.       while (p1 && p2)
  2872.     {
  2873.       new_harshness = convert_harshness (TREE_VALUE (p1), TREE_VALUE (p2), 0);
  2874.       if (new_harshness & 1)
  2875.         return 1;
  2876.       if ((new_harshness & 7) == 0)
  2877.         harshness += new_harshness;
  2878.       else
  2879.         harshness |= new_harshness;
  2880.       p1 = TREE_CHAIN (p1);
  2881.       p2 = TREE_CHAIN (p2);
  2882.     }
  2883.       if (p1 == p2)
  2884.     return harshness;
  2885.       if (p2)
  2886.     return 1;
  2887.       if (p1)
  2888.     return harshness | (TREE_PURPOSE (p1) == NULL_TREE);
  2889.     }
  2890.   else if (codel == POINTER_TYPE && coder == OFFSET_TYPE)
  2891.     {
  2892.       int harshness;
  2893.  
  2894.       /* Get to the OFFSET_TYPE that this might be.  */
  2895.       type = TREE_TYPE (type);
  2896.  
  2897.       if (coder != TREE_CODE (type))
  2898.     return 1;
  2899.  
  2900.       harshness = 0;
  2901.  
  2902.       if (TYPE_OFFSET_BASETYPE (type) == TYPE_OFFSET_BASETYPE (parmtype))
  2903.     harshness = 0;
  2904.       else if (get_base_type (TYPE_OFFSET_BASETYPE (type),
  2905.                   TYPE_OFFSET_BASETYPE (parmtype), 0))
  2906.     harshness = (1<<3);
  2907.       else
  2908.     return 1;
  2909.       /* Now test the OFFSET_TYPE's target compatability.  */
  2910.       type = TREE_TYPE (type);
  2911.       parmtype = TREE_TYPE (parmtype);
  2912.     }
  2913.  
  2914.   if (coder == UNKNOWN_TYPE)
  2915.     {
  2916.       if (codel == FUNCTION_TYPE
  2917.       || codel == METHOD_TYPE
  2918.       || (codel == POINTER_TYPE
  2919.           && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
  2920.           || TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE)))
  2921.     return 0;
  2922.       return 1;
  2923.     }
  2924.  
  2925.   if (coder == VOID_TYPE)
  2926.     return 1;
  2927.  
  2928.   if (codel == ENUMERAL_TYPE || codel == INTEGER_TYPE)
  2929.     {
  2930.       /* Control equivalence of ints an enums.  */
  2931.  
  2932.       if (codel == ENUMERAL_TYPE
  2933.       && flag_int_enum_equivalence == 0)
  2934.     {
  2935.       /* Enums can be converted to ints, but not vice-versa.  */
  2936.       if (coder != ENUMERAL_TYPE
  2937.           || TYPE_MAIN_VARIANT (type) != TYPE_MAIN_VARIANT (parmtype))
  2938.         return 1;
  2939.     }
  2940.  
  2941.       /* else enums and ints (almost) freely interconvert.  */
  2942.  
  2943.       if (coder == INTEGER_TYPE || coder == ENUMERAL_TYPE)
  2944.     {
  2945.       int easy = TREE_UNSIGNED (type) ^ TREE_UNSIGNED (parmtype);
  2946.       if (codel != coder)
  2947.         easy += 1;
  2948.       if (TYPE_MODE (type) != TYPE_MODE (parmtype))
  2949.         easy += 2;
  2950.       return (easy << 4);
  2951.     }
  2952.       else if (coder == REAL_TYPE)
  2953.     return (4<<4);
  2954.     }
  2955.  
  2956.   if (codel == REAL_TYPE)
  2957.     if (coder == REAL_TYPE)
  2958.       /* Shun converting between float and double if a choice exists.  */
  2959.       {
  2960.     if (TYPE_MODE (type) != TYPE_MODE (parmtype))
  2961.       return (2<<4);
  2962.     return 0;
  2963.       }
  2964.     else if (coder == INTEGER_TYPE || coder == ENUMERAL_TYPE)
  2965.       return (4<<4);
  2966.  
  2967.   /* convert arrays which have not previously been converted.  */
  2968.   if (codel == ARRAY_TYPE)
  2969.     codel = POINTER_TYPE;
  2970.   if (coder == ARRAY_TYPE)
  2971.     coder = POINTER_TYPE;
  2972.  
  2973.   /* Conversions among pointers */
  2974.   if (codel == POINTER_TYPE && coder == POINTER_TYPE)
  2975.     {
  2976.       register tree ttl = TYPE_MAIN_VARIANT (TREE_TYPE (type));
  2977.       register tree ttr = TYPE_MAIN_VARIANT (TREE_TYPE (parmtype));
  2978.       int penalty = 4 * (ttl != ttr);
  2979.       /* Anything converts to void *.  void * converts to anything.
  2980.      Since these may be `const void *' (etc.) use VOID_TYPE
  2981.      instead of void_type_node.
  2982.      Otherwise, the targets must be the same,
  2983.      except that we do allow (at some cost) conversion
  2984.      between signed and unsinged pointer types.  */
  2985.  
  2986.       if ((TREE_CODE (ttl) == METHOD_TYPE
  2987.        || TREE_CODE (ttl) == FUNCTION_TYPE)
  2988.       && TREE_CODE (ttl) == TREE_CODE (ttr))
  2989.     {
  2990.       if (comptypes (ttl, ttr, -1))
  2991.         return penalty<<4;
  2992.       return 1;
  2993.     }
  2994.  
  2995.       if (!(TREE_CODE (ttl) == VOID_TYPE
  2996.         || TREE_CODE (ttr) == VOID_TYPE
  2997.         || (TREE_UNSIGNED (ttl) ^ TREE_UNSIGNED (ttr)
  2998.         && (ttl = unsigned_type (ttl),
  2999.             ttr = unsigned_type (ttr),
  3000.             penalty = 10, 0))
  3001.         || (comp_target_types (ttl, ttr, 0))))
  3002.     return 1;
  3003.  
  3004.       if (ttr == ttl)
  3005.     return 4;
  3006.  
  3007.       if (IS_AGGR_TYPE (ttl) && IS_AGGR_TYPE (ttr))
  3008.     {
  3009.       int b_or_d = get_base_distance (ttl, ttr, 0, 0);
  3010.       if (b_or_d < 0)
  3011.         return 1;
  3012.       return (b_or_d<<3) | 4;
  3013.     }
  3014.  
  3015.       return (penalty<<4);
  3016.     }
  3017.  
  3018.   if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
  3019.     {
  3020.       /* This is not a bad match, but don't let it beat
  3021.      integer-enum combinations.  */
  3022.       if (parm && integer_zerop (parm))
  3023.     return (4<<4);
  3024.     }
  3025.  
  3026.   /* C++: one of the types must be a reference type.  */
  3027.   {
  3028.     tree ttl, ttr;
  3029.     register tree intype = TYPE_MAIN_VARIANT (parmtype);
  3030.     register enum tree_code form = TREE_CODE (intype);
  3031.     int penalty;
  3032.  
  3033.     if (codel == REFERENCE_TYPE || coder == REFERENCE_TYPE)
  3034.       {
  3035.     ttl = TYPE_MAIN_VARIANT (type);
  3036.  
  3037.     if (codel == REFERENCE_TYPE)
  3038.       {
  3039.         ttl = TYPE_MAIN_VARIANT (TREE_TYPE (ttl));
  3040.  
  3041.         if (form == OFFSET_TYPE)
  3042.           {
  3043.         intype = TREE_TYPE (intype);
  3044.         form = TREE_CODE (intype);
  3045.           }
  3046.  
  3047.         if (form == REFERENCE_TYPE)
  3048.           {
  3049.         intype = TYPE_MAIN_VARIANT (TREE_TYPE (intype));
  3050.  
  3051.         if (ttl == intype)
  3052.           return 0;
  3053.         penalty = 2;
  3054.           }
  3055.         else
  3056.           {
  3057.         /* Can reference be built up?  */
  3058.         if (ttl == intype)
  3059.           {
  3060.             return 0;
  3061.           }
  3062.         else
  3063.           penalty = 2;
  3064.           }
  3065.       }
  3066.     else if (form == REFERENCE_TYPE)
  3067.       {
  3068.         if (parm)
  3069.           {
  3070.         tree tmp = convert_from_reference (parm);
  3071.         intype = TYPE_MAIN_VARIANT (TREE_TYPE (tmp));
  3072.           }
  3073.         else
  3074.           {
  3075.         intype = parmtype;
  3076.         do
  3077.           {
  3078.             intype = TREE_TYPE (intype);
  3079.           }
  3080.         while (TREE_CODE (intype) == REFERENCE_TYPE);
  3081.         intype = TYPE_MAIN_VARIANT (intype);
  3082.           }
  3083.  
  3084.         if (ttl == intype)
  3085.           return 0;
  3086.         else
  3087.           penalty = 2;
  3088.       }
  3089.  
  3090.     if (TREE_UNSIGNED (ttl) ^ TREE_UNSIGNED (intype))
  3091.       {
  3092.         ttl = unsigned_type (ttl);
  3093.         intype = unsigned_type (intype);
  3094.         penalty += 2;
  3095.       }
  3096.  
  3097.     ttr = intype;
  3098.  
  3099.     /* If the initializer is not an lvalue, then it does not
  3100.        matter if we make life easier for the programmer
  3101.        by creating a temporary variable with which to
  3102.        hold the result.  */
  3103.     if (parm && (coder == INTEGER_TYPE
  3104.              || coder == ENUMERAL_TYPE
  3105.              || coder == REAL_TYPE)
  3106.         && ! lvalue_p (parm))
  3107.       return (convert_harshness (ttl, ttr, 0) | (penalty << 4));
  3108.  
  3109.     if (ttl == ttr)
  3110.       return 4;
  3111.  
  3112.     /* Pointers to voids always convert for pointers.  But
  3113.        make them less natural than more specific matches.  */
  3114.     if (TREE_CODE (ttl) == POINTER_TYPE && TREE_CODE (ttr) == POINTER_TYPE)
  3115.       if (TREE_TYPE (ttl) == void_type_node
  3116.           || TREE_TYPE (ttr) == void_type_node)
  3117.         return ((penalty+1)<<4);
  3118.  
  3119.     if (parm && codel != REFERENCE_TYPE)
  3120.       return (convert_harshness (ttl, ttr, 0) | (penalty << 4));
  3121.  
  3122.     /* Here it does matter.  If this conversion is from
  3123.        derived to base, allow it.  Otherwise, types must
  3124.        be compatible in the strong sense.  */
  3125.     if (IS_AGGR_TYPE (ttl) && IS_AGGR_TYPE (ttr))
  3126.       {
  3127.         int b_or_d = get_base_distance (ttl, ttr, 0, 0);
  3128.         if (b_or_d < 0)
  3129.           return 1;
  3130. #if AMBIGUOUS_WORKING
  3131.         if (ttl == TYPE_MAIN_VARIANT (type)
  3132.         && TYPE_GETS_INIT_REF (type))
  3133.           return (b_or_d<<3) | 6;
  3134. #endif
  3135.         return (b_or_d<<3) | 4;
  3136.       }
  3137.  
  3138.     if (comp_target_types (ttl, intype, 1))
  3139.       return (penalty<<4);
  3140.       }
  3141.   }
  3142.   if (codel == RECORD_TYPE && coder == RECORD_TYPE)
  3143.     {
  3144.       int b_or_d = get_base_distance (type, parmtype, 0, 0);
  3145.       if (b_or_d < 0)
  3146.     return 1;
  3147. #if AMBIGUOUS_WORKING
  3148.       if (TYPE_GETS_INIT_REF (type))
  3149.     return (b_or_d<<3) | 6;
  3150. #endif
  3151.       return (b_or_d<<3) | 4;
  3152.     }
  3153.   return 1;
  3154. }
  3155.  
  3156. /* Algorithm: Start out with no stikes against.  For each argument
  3157.    which requires a (subjective) hard conversion (such as between
  3158.    floating point and integer), issue a strike.  If there are the same
  3159.    number of formal and actual parameters in the list, there will be at
  3160.    least on strike, otherwise an exact match would have been found.  If
  3161.    there are not the same number of arguments in the type lists, we are
  3162.    not dead yet: a `...' means that we can have more parms then were
  3163.    declared, and if we wind up in the default argument section of the
  3164.    list those can be used as well.  If an exact match could be found for
  3165.    one of those cases, return it immediately.  Otherwise, Rank the fields
  3166.    so that fields with fewer strikes are tried first.
  3167.  
  3168.    Conversions between builtin and user-defined types are allowed, but
  3169.    no function involving such a conversion is prefered to one which
  3170.    does not require such a conversion.  Furthermore, such conversions
  3171.    must be unique.  */
  3172.  
  3173. void
  3174. compute_conversion_costs (function, tta_in, cp, arglen)
  3175.      tree function;
  3176.      tree tta_in;
  3177.      struct candidate *cp;
  3178.      int arglen;
  3179. {
  3180.   tree ttf_in = TYPE_ARG_TYPES (TREE_TYPE (function));
  3181.   tree ttf = ttf_in;
  3182.   tree tta = tta_in;
  3183.  
  3184.   /* Start out with no strikes against.  */
  3185.   int evil_strikes = 0;
  3186.   int user_strikes = 0;
  3187.   int b_or_d_strikes = 0;
  3188.   int easy_strikes = 0;
  3189.  
  3190.   int strike_index = 0, win, lose;
  3191.  
  3192. #ifdef GATHER_STATISTICS
  3193.   n_compute_conversion_costs++;
  3194. #endif
  3195.  
  3196.   cp->function = function;
  3197.   cp->arg = tta ? TREE_VALUE (tta) : NULL_TREE;
  3198.   cp->u.bad_arg = 0;        /* optimistic!  */
  3199.  
  3200.   bzero (cp->harshness, (arglen+1) * sizeof (short));
  3201.  
  3202.   while (ttf && tta)
  3203.     {
  3204.       int harshness;
  3205.  
  3206.       if (ttf == void_list_node)
  3207.     break;
  3208.  
  3209.       if (type_unknown_p (TREE_VALUE (tta)))
  3210.     {      
  3211.       /* Must perform some instantiation here.  */
  3212.       tree rhs = TREE_VALUE (tta);
  3213.       tree lhstype = TREE_VALUE (ttf);
  3214.  
  3215.       /* @@ This is to undo what `grokdeclarator' does to
  3216.          parameter types.  It really should go through
  3217.          something more general.  */
  3218.  
  3219.       TREE_TYPE (tta) = unknown_type_node;
  3220.       if (TREE_CODE (rhs) == OP_IDENTIFIER)
  3221.         rhs = build_instantiated_decl (lhstype, rhs);
  3222.       else
  3223.         {
  3224.           /* Keep quiet about possible contravariance violations.  */
  3225.           extern int inhibit_warnings;
  3226.           int old_inhibit_warnings = inhibit_warnings;
  3227.           inhibit_warnings = 1;
  3228.  
  3229.           rhs = instantiate_type (lhstype, rhs, 0);
  3230.  
  3231.           inhibit_warnings = old_inhibit_warnings;
  3232.         }
  3233.  
  3234.       if (TREE_CODE (rhs) == ERROR_MARK)
  3235.         harshness = 1;
  3236.       else
  3237.         {
  3238.           harshness = convert_harshness (lhstype, TREE_TYPE (rhs), rhs);
  3239.           /* harshness |= 2; */
  3240.         }
  3241.     }
  3242.       else
  3243.     harshness = convert_harshness (TREE_VALUE (ttf), TREE_TYPE (TREE_VALUE (tta)), TREE_VALUE (tta));
  3244.  
  3245.       cp->harshness[strike_index] = harshness;
  3246.       if (harshness & 1)
  3247.     {
  3248.       cp->u.bad_arg = strike_index;
  3249.       evil_strikes = 1;
  3250.     }
  3251.       else if (harshness & 2)
  3252.     {
  3253.       user_strikes += 1;
  3254.     }
  3255.       else if (harshness & 4)
  3256.     {
  3257.       b_or_d_strikes += (harshness >> 3);
  3258.     }
  3259.       else
  3260.     easy_strikes += harshness >> 4;
  3261.       ttf = TREE_CHAIN (ttf);
  3262.       tta = TREE_CHAIN (tta);
  3263.       strike_index += 1;
  3264.     }
  3265.  
  3266.   if (tta)
  3267.     {
  3268.       /* ran out of formals, and parmlist is fixed size.  */
  3269.       if (ttf /* == void_type_node */)
  3270.     {
  3271.       cp->evil = 1;
  3272.       cp->u.bad_arg = -1;
  3273.       return;
  3274.     }
  3275.     }
  3276.   else if (ttf && ttf != void_list_node)
  3277.     {
  3278.       /* ran out of actuals, and no defaults.  */
  3279.       if (TREE_PURPOSE (ttf) == NULL_TREE)
  3280.     {
  3281.       cp->evil = 1;
  3282.       cp->u.bad_arg = -2;
  3283.       return;
  3284.     }
  3285.       /* Store index of first default.  */
  3286.       cp->harshness[arglen] = strike_index+1;
  3287.     }
  3288.   else cp->harshness[arglen] = 0;
  3289.  
  3290.   /* Argument list lengths work out, so don't need to check them again.  */
  3291.   if (evil_strikes)
  3292.     {
  3293.       /* We do not check for derived->base conversions here, since in
  3294.      no case would they give evil strike counts, unless such conversions
  3295.      are somehow ambiguous.  */
  3296.  
  3297.       /* See if any user-defined conversions apply.
  3298.          But make sure that we do not loop.  */
  3299.       static int dont_convert_types = 0;
  3300.  
  3301.       if (dont_convert_types)
  3302.     {
  3303.       cp->evil = 1;
  3304.       return;
  3305.     }
  3306.  
  3307.       win = 0;            /* Only get one chance to win.  */
  3308.       ttf = TYPE_ARG_TYPES (TREE_TYPE (function));
  3309.       tta = tta_in;
  3310.       strike_index = 0;
  3311.       evil_strikes = 0;
  3312.  
  3313.       while (ttf && tta)
  3314.     {
  3315.       if (ttf == void_list_node)
  3316.         break;
  3317.  
  3318.       lose = cp->harshness[strike_index];
  3319.       if (lose&1)
  3320.         {
  3321.           tree actual_type = TREE_TYPE (TREE_VALUE (tta));
  3322.           tree formal_type = TREE_VALUE (ttf);
  3323.  
  3324.           dont_convert_types = 1;
  3325.  
  3326.           if (TREE_CODE (formal_type) == REFERENCE_TYPE)
  3327.         formal_type = TREE_TYPE (formal_type);
  3328.           if (TREE_CODE (actual_type) == REFERENCE_TYPE)
  3329.         actual_type = TREE_TYPE (actual_type);
  3330.  
  3331.           if (formal_type != error_mark_node
  3332.           && actual_type != error_mark_node)
  3333.         {
  3334.           formal_type = TYPE_MAIN_VARIANT (formal_type);
  3335.           actual_type = TYPE_MAIN_VARIANT (actual_type);
  3336.  
  3337.           if (TYPE_HAS_CONSTRUCTOR (formal_type))
  3338.             {
  3339.               /* If it has a constructor for this type, try to use it.  */
  3340.               if (convert_to_aggr (formal_type, TREE_VALUE (tta), 0, 1)
  3341.               != error_mark_node)
  3342.             {
  3343.               /* @@ There is no way to save this result yet.
  3344.                  @@ So success is NULL_TREE for now.  */
  3345.               win++;
  3346.             }
  3347.             }
  3348.           if (TYPE_LANG_SPECIFIC (actual_type) && TYPE_HAS_CONVERSION (actual_type))
  3349.             {
  3350.               if (TREE_CODE (formal_type) == INTEGER_TYPE
  3351.               && TYPE_HAS_INT_CONVERSION (actual_type))
  3352.             win++;
  3353.               else if (TREE_CODE (formal_type) == REAL_TYPE
  3354.                    && TYPE_HAS_REAL_CONVERSION (actual_type))
  3355.             win++;
  3356.               else
  3357.             {
  3358.               tree conv = build_type_conversion (CALL_EXPR, TREE_VALUE (ttf), TREE_VALUE (tta), 0);
  3359.               if (conv)
  3360.                 if (conv == error_mark_node)
  3361.                   win += 2;
  3362.                 else
  3363.                   win++;
  3364.             }
  3365.             }
  3366.         }
  3367.           dont_convert_types = 0;
  3368.  
  3369.           if (win == 1)
  3370.         {
  3371.           user_strikes += 1;
  3372.           cp->harshness[strike_index] = 2;
  3373.           win = 0;
  3374.         }
  3375.           else
  3376.         {
  3377.           if (cp->u.bad_arg > strike_index)
  3378.             cp->u.bad_arg = strike_index;
  3379.  
  3380.           evil_strikes = win ? 2 : 1;
  3381.           break;
  3382.         }
  3383.         }
  3384.  
  3385.       ttf = TREE_CHAIN (ttf);
  3386.       tta = TREE_CHAIN (tta);
  3387.       strike_index += 1;
  3388.     }
  3389.     }
  3390.  
  3391.   /* Calling a non-const member function from a const member function
  3392.      is probably invalid, but for now we let it only draw a warning.
  3393.      We indicate that such a mismatch has occured by setting the
  3394.      harshness to a maximum value.  */
  3395.   if (TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE
  3396.       && TREE_CODE (TREE_TYPE (TREE_VALUE (tta_in))) == POINTER_TYPE
  3397.       && (TREE_READONLY (TREE_TYPE (TREE_TYPE (TREE_VALUE (tta_in))))
  3398.       > TREE_READONLY (TREE_TYPE (TREE_VALUE (ttf_in)))))
  3399.     cp->harshness[0] |= 128;
  3400.  
  3401.   cp->evil = evil_strikes;
  3402.   cp->user = user_strikes;
  3403.   cp->b_or_d = b_or_d_strikes;
  3404.   cp->easy = easy_strikes;
  3405. }
  3406.  
  3407. struct candidate *
  3408. ideal_candidate (basetype, candidates, n_candidates, parms, len)
  3409.      tree basetype;
  3410.      struct candidate *candidates;
  3411.      int n_candidates;
  3412.      tree parms;
  3413.      int len;
  3414. {
  3415.   struct candidate *cp = candidates + n_candidates;
  3416.   int index, i;
  3417.   tree ttf;
  3418.  
  3419.   qsort (candidates,        /* char *base */
  3420.      n_candidates,        /* int nel */
  3421.      sizeof (struct candidate), /* int width */
  3422.      rank_for_overload);    /* int (*compar)() */
  3423.  
  3424.   /* If the best candidate requires user-defined conversions,
  3425.      and its user-defined conversions are a strict subset
  3426.      of all other candidates requiring user-defined conversions,
  3427.      then it is, in fact, the best.  */
  3428.   for (i = -1; cp + i != candidates; i--)
  3429.     if (cp[i].user == 0)
  3430.       break;
  3431.  
  3432.   if (i < -1)
  3433.     {
  3434.       tree ttf0;
  3435.  
  3436.       /* Check that every other candidate requires those conversions
  3437.      as a strict subset of their conversions.  */
  3438.       if (cp[i].user == cp[-1].user)
  3439.     goto non_subset;
  3440.  
  3441.       /* Look at subset relationship more closely.  */
  3442.       while (i != -1)
  3443.     {
  3444.       for (ttf = TYPE_ARG_TYPES (TREE_TYPE (cp[i].function)),
  3445.            ttf0 = TYPE_ARG_TYPES (TREE_TYPE (cp[-1].function)),
  3446.            index = 0;
  3447.            index < len;
  3448.            ttf = TREE_CHAIN (ttf), ttf0 = TREE_CHAIN (ttf0), index++)
  3449.         if (cp[i].harshness[index] & 2)
  3450.           {
  3451.         /* If our "best" candidate also needs a conversion,
  3452.            it must be the same one.  */
  3453.         if ((cp[-1].harshness[index] & 2)
  3454.             && TREE_VALUE (ttf) != TREE_VALUE (ttf0))
  3455.           goto non_subset;
  3456.           }
  3457.       i++;
  3458.     }
  3459.       /* The best was the best.  */
  3460.       return cp - 1;
  3461.     non_subset:
  3462.       /* Use other rules for determining "bestness".  */
  3463.       ;
  3464.     }
  3465.  
  3466.   /* If the best two candidates we find require user-defined
  3467.      conversions, we may need to report and error message.  */
  3468.   if (cp[-1].user && cp[-2].user
  3469.       && (cp[-1].b_or_d || cp[-2].b_or_d == 0))
  3470.     {
  3471.       /* If the best two methods found involved user-defined
  3472.      type conversions, then we must see whether one
  3473.      of them is exactly what we wanted.  If not, then
  3474.      we have an ambiguity.  */
  3475.       int best = 0;
  3476.       tree tta = parms;
  3477.       tree f1, p1;
  3478.  
  3479. #if AMBIGUOUS_WORKING
  3480.       if (cp[-1].b_or_d == 0
  3481.       && cp[-1].easy == 0
  3482.       && (cp[-2].b_or_d | cp[-2].easy) > 0)
  3483.     return cp - 1;
  3484. #endif
  3485.  
  3486.       /* Stash all of our parameters in safe places
  3487.      so that we can perform type conversions in place.  */
  3488.       while (tta)
  3489.     {
  3490.       TREE_PURPOSE (tta) = TREE_VALUE (tta);
  3491.       tta = TREE_CHAIN (tta);
  3492.     }
  3493.  
  3494.       i = 0;
  3495.       do
  3496.     {
  3497.       int exact_conversions = 0;
  3498.  
  3499.       i -= 1;
  3500.       tta = parms;
  3501.       if (DECL_STATIC_FUNCTION_P (cp[i].function))
  3502.         tta = TREE_CHAIN (tta);
  3503.       for (ttf = TYPE_ARG_TYPES (TREE_TYPE (cp[i].function)), index = 0;
  3504.            index < len;
  3505.            tta = TREE_CHAIN (tta), ttf = TREE_CHAIN (ttf), index++)
  3506.         {
  3507.           if (cp[i].harshness[index] & 2)
  3508.         {
  3509.           TREE_VALUE (tta)
  3510.             = build_type_conversion (CALL_EXPR, TREE_VALUE (ttf), TREE_PURPOSE (tta), 2);
  3511.           if (TREE_VALUE (tta))
  3512.             {
  3513.               if (TREE_CODE (TREE_VALUE (tta)) != CONVERT_EXPR
  3514.               && (TREE_CODE (TREE_VALUE (tta)) != NOP_EXPR
  3515.                   || comp_target_types (TREE_TYPE (TREE_VALUE (tta)),
  3516.                             TREE_TYPE (TREE_OPERAND (TREE_VALUE (tta), 0)), 1)))
  3517.             exact_conversions += 1;
  3518.             }
  3519.           else if (IS_AGGR_TYPE (TREE_VALUE (ttf))
  3520.                || (TREE_CODE (TREE_VALUE (ttf)) == REFERENCE_TYPE
  3521.                    && IS_AGGR_TYPE (TREE_TYPE (TREE_VALUE (ttf)))))
  3522.             {
  3523.               /* To get here we had to have succeeded via
  3524.              a constructor.  */
  3525.               TREE_VALUE (tta) = TREE_PURPOSE (tta);
  3526.               exact_conversions += 1;
  3527.             }
  3528.         }
  3529.         }
  3530.       if (exact_conversions == cp[i].user)
  3531.         {
  3532.           if (best == 0)
  3533.         {
  3534.           best = i;
  3535.           f1 = cp[best].function;
  3536.           p1 = TYPE_ARG_TYPES (TREE_TYPE (f1));
  3537.         }
  3538.           else
  3539.         {
  3540.           /* Don't complain if next best is from base class.  */
  3541.           tree f2 = cp[i].function;
  3542.           tree p2 = TYPE_ARG_TYPES (TREE_TYPE (f2));
  3543.  
  3544.           if (TREE_CODE (TREE_TYPE (f1)) == METHOD_TYPE
  3545.               && TREE_CODE (TREE_TYPE (f2)) == METHOD_TYPE
  3546.               && (cp[i].harshness[0] & 4) != 0
  3547.               && cp[best].harshness[0] < cp[i].harshness[0])
  3548.             {
  3549.               if (! compparms (TREE_CHAIN (p1), TREE_CHAIN (p2), 1))
  3550.             goto ret0;
  3551.               else
  3552.             continue;
  3553.             }
  3554.           else goto ret0;
  3555.         }
  3556.         }
  3557.     } while (cp + i != candidates);
  3558.  
  3559.       if (best)
  3560.     {
  3561.       int exact_conversions = cp[best].user;
  3562.       tta = parms;
  3563.       if (DECL_STATIC_FUNCTION_P (cp[best].function))
  3564.         tta = TREE_CHAIN (parms);
  3565.       for (ttf = TYPE_ARG_TYPES (TREE_TYPE (cp[best].function)), index = 0;
  3566.            exact_conversions > 0;
  3567.            tta = TREE_CHAIN (tta), ttf = TREE_CHAIN (ttf), index++)
  3568.         if (cp[best].harshness[index] & 2)
  3569.           {
  3570.         /* We must now fill in the slot we left behind.
  3571.            @@ This could be optimized to use the value previously
  3572.            @@ computed by build_type_conversion in some cases.  */
  3573.         TREE_VALUE (tta) = convert (TREE_VALUE (ttf), TREE_PURPOSE (tta));
  3574.         exact_conversions -= 1;
  3575.           }
  3576.       return cp + best;
  3577.     }
  3578.       goto ret0;
  3579.     }
  3580.   /* If the best two candidates we find both use default parameters,
  3581.      we may need to report and error.  Don't need to worry if next-best
  3582.      candidate is forced to use user-defined conversion when best is not.  */
  3583.   if (cp[-2].user == 0
  3584.       && cp[-1].harshness[len] != 0 && cp[-2].harshness[len] != 0)
  3585.     {
  3586.       tree tt1 = TYPE_ARG_TYPES (TREE_TYPE (cp[-1].function));
  3587.       tree tt2 = TYPE_ARG_TYPES (TREE_TYPE (cp[-2].function));
  3588.       int i = cp[-1].harshness[len];
  3589.       if (cp[-2].harshness[len] < i)
  3590.     i = cp[-2].harshness[len];
  3591.       while (--i > 0)
  3592.     {
  3593.       if (TYPE_MAIN_VARIANT (TREE_VALUE (tt1))
  3594.           != TYPE_MAIN_VARIANT (TREE_VALUE (tt2)))
  3595.         /* These lists are not identical, so we can choose our best candidate.  */
  3596.         return cp - 1;
  3597.       tt1 = TREE_CHAIN (tt1);
  3598.       tt2 = TREE_CHAIN (tt2);
  3599.     }
  3600.       /* To get here, both lists had the same parameters up to the defaults
  3601.      which were used.  This is an ambiguous request.  */
  3602.       goto ret0;
  3603.     }
  3604.  
  3605.   /* Otherwise, return our best candidate.  Note that if we get candidates
  3606.      from independent base classes, we have an ambiguity, even if one
  3607.      argument list look a little better than another one.  */
  3608.   if (cp[-1].b_or_d && basetype && TYPE_USES_MULTIPLE_INHERITANCE (basetype))
  3609.     {
  3610.       int i = n_candidates - 1, best;
  3611.       tree base1 = NULL_TREE;
  3612.  
  3613.       if (TREE_CODE (TREE_TYPE (candidates[i].function)) == FUNCTION_TYPE)
  3614.     return cp - 1;
  3615.  
  3616.       for (; i >= 0 && candidates[i].user == 0 && candidates[i].evil == 0; i--)
  3617.     {
  3618.       if (TREE_CODE (TREE_TYPE (candidates[i].function)) == METHOD_TYPE)
  3619.         {
  3620.           tree newbase = TYPE_METHOD_BASETYPE (TREE_TYPE (candidates[i].function));
  3621.  
  3622.           if (base1 != NULL_TREE)
  3623.         {
  3624.           if (newbase != base1
  3625.               && ! get_base_type (newbase, base1, 0))
  3626.             {
  3627.               char *buf = (char *)alloca (8192);
  3628.               error ("ambiguous request for function from distinct base classes of type `%s'", TYPE_NAME_STRING (basetype));
  3629.               error ("first candidate is `%s'", fndecl_as_string (buf, 0, candidates[best].function, 1));
  3630.               error ("second candidates is `%s'", fndecl_as_string (buf, 0, candidates[i].function, 1));
  3631.               return cp - 1;
  3632.             }
  3633.         }
  3634.           else
  3635.         {
  3636.           best = i;
  3637.           base1 = newbase;
  3638.         }
  3639.         }
  3640.       else return cp - 1;
  3641.     }
  3642.     }
  3643.  
  3644. #if AMBIGUOUS_WORKING
  3645.   if (cp[-1].user == cp[-2].user
  3646.       && cp[-1].b_or_d == cp[-2].b_or_d
  3647.       && cp[-1].easy == cp[-2].easy)
  3648.     goto ret0;
  3649. #endif
  3650.  
  3651.   return cp - 1;
  3652.  
  3653.  ret0:
  3654.   /* In the case where there is no ideal candidate, restore
  3655.      TREE_VALUE slots of PARMS from TREE_PURPOSE slots.  */
  3656.   while (parms)
  3657.     {
  3658.       TREE_VALUE (parms) = TREE_PURPOSE (parms);
  3659.       parms = TREE_CHAIN (parms);
  3660.     }
  3661.   return 0;
  3662. }
  3663.  
  3664. /* Assume that if the class referred to is not in the
  3665.    current class hierarchy, that it may be remote.
  3666.    PARENT is assumed to be of aggregate type here.  */
  3667. static int
  3668. may_be_remote (parent)
  3669.      tree parent;
  3670. {
  3671.   if (TYPE_OVERLOADS_METHOD_CALL_EXPR (parent) == 0)
  3672.     return 0;
  3673.  
  3674.   if (current_class_type == NULL_TREE)
  3675.     return 0;
  3676.   if (parent == current_class_type)
  3677.     return 0;
  3678.  
  3679.   if (get_base_type (parent, current_class_type, 0))
  3680.     return 0;
  3681.   return 1;
  3682. }
  3683.  
  3684. /* Return the number of bytes that the arglist in PARMS would
  3685.    occupy on the stack.  */
  3686. int
  3687. get_arglist_len_in_bytes (parms)
  3688.      tree parms;
  3689. {
  3690.   register tree parm;
  3691.   register int bytecount = 0;
  3692.  
  3693.   for (parm = parms; parm; parm = TREE_CHAIN (parm))
  3694.     {
  3695.       register tree pval = TREE_VALUE (parm);
  3696.       register int used, size;
  3697.  
  3698.       if (TREE_CODE (pval) == ERROR_MARK)
  3699.     continue;
  3700.       else if (TYPE_MODE (TREE_TYPE (pval)) != BLKmode)
  3701.     {
  3702.       used = size = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (pval)));
  3703. #ifdef PUSH_ROUNDING
  3704.       size = PUSH_ROUNDING (size);
  3705. #endif
  3706.       used = (((size + PARM_BOUNDARY / BITS_PER_UNIT - 1)
  3707.            / (PARM_BOUNDARY / BITS_PER_UNIT))
  3708.           * (PARM_BOUNDARY / BITS_PER_UNIT));
  3709.     }
  3710.       else
  3711.     {
  3712.       register tree size = size_in_bytes (TREE_TYPE (pval));
  3713.       register tree used_t = convert_units (convert_units (size, BITS_PER_UNIT, PARM_BOUNDARY),
  3714.                         PARM_BOUNDARY, BITS_PER_UNIT);
  3715.       used = TREE_INT_CST_LOW (used_t);
  3716.     }
  3717.       bytecount += used;
  3718.     }
  3719.   return bytecount;
  3720. }
  3721.  
  3722. tree
  3723. build_vfield_ref (datum, type)
  3724.      tree datum, type;
  3725. {
  3726.   if (TREE_CODE (TREE_TYPE (datum)) == REFERENCE_TYPE)
  3727.     datum = convert_from_reference (datum);
  3728.  
  3729.   if (! TYPE_USES_VIRTUAL_BASECLASSES (type))
  3730.     return build (COMPONENT_REF, TREE_TYPE (CLASSTYPE_VFIELD (type)),
  3731.           datum, CLASSTYPE_VFIELD (type));
  3732.   return build_component_ref (datum, DECL_NAME (CLASSTYPE_VFIELD (type)), 0, 0);
  3733. }
  3734.  
  3735. /* Build a call to a member of an object.  I.e., one that overloads
  3736.    operator ()(), or is a pointer-to-function or pointer-to-method.  */
  3737. static tree
  3738. build_field_call (basetype_path, instance_ptr, name, parms, err_name)
  3739.      tree basetype_path;
  3740.      tree instance_ptr, name, parms;
  3741.      char *err_name;
  3742. {
  3743.   tree field, instance;
  3744.  
  3745.   if (instance_ptr == current_class_decl)
  3746.     {
  3747.       /* Check to see if we really have a reference to an instance variable
  3748.      with `operator()()' overloaded.  */
  3749. #if 1
  3750.       field = IDENTIFIER_CLASS_VALUE (name);
  3751. #else
  3752.       field = identifier_class_value (name);
  3753. #endif
  3754.  
  3755.       if (field == NULL_TREE)
  3756.     {
  3757.       error ("`this' has no member named `%s'", err_name);
  3758.       return error_mark_node;
  3759.     }
  3760.  
  3761.       if (TREE_CODE (field) == FIELD_DECL)
  3762.     {
  3763.       /* If it's a field, try overloading operator (),
  3764.          or calling if the field is a pointer-to-function.  */
  3765.       instance = build_component_ref_1 (C_C_D, field, 0, 1);
  3766.       if (instance == error_mark_node)
  3767.         return error_mark_node;
  3768.  
  3769.       if (TYPE_LANG_SPECIFIC (TREE_TYPE (instance))
  3770.           && TYPE_OVERLOADS_CALL_EXPR (TREE_TYPE (instance)))
  3771.         return build_opfncall (CALL_EXPR, LOOKUP_NORMAL, instance, parms);
  3772.  
  3773.       if (TREE_CODE (TREE_TYPE (instance)) == POINTER_TYPE)
  3774.         if (TREE_CODE (TREE_TYPE (TREE_TYPE (instance))) == FUNCTION_TYPE)
  3775.           return build_function_call (instance, parms);
  3776.         else if (TREE_CODE (TREE_TYPE (TREE_TYPE (instance))) == METHOD_TYPE)
  3777.           return build_function_call (instance, tree_cons (NULL_TREE, current_class_decl, parms));
  3778.     }
  3779.       return NULL_TREE;
  3780.     }
  3781.  
  3782.   /* Check to see if this is not really a reference to an instance variable
  3783.      with `operator()()' overloaded.  */
  3784.   field = lookup_field (basetype_path, name, 1);
  3785.  
  3786.   /* This can happen if the reference was ambiguous
  3787.      or for visibility violations.  */
  3788.   if (field == error_mark_node)
  3789.     return error_mark_node;
  3790.   if (field)
  3791.     {
  3792.       tree basetype;
  3793.       tree ftype = TREE_TYPE (field);
  3794.  
  3795.       if (TYPE_LANG_SPECIFIC (ftype) && TYPE_OVERLOADS_CALL_EXPR (ftype))
  3796.     {
  3797.       /* Make the next search for this field very short.  */
  3798.       basetype = DECL_FIELD_CONTEXT (field);
  3799.       instance_ptr = convert_pointer_to (basetype, instance_ptr);
  3800.  
  3801.       instance = build_indirect_ref (instance_ptr, 0);
  3802.       return build_opfncall (CALL_EXPR, LOOKUP_NORMAL,
  3803.                  build_component_ref_1 (instance, field, 0, 0),
  3804.                  parms);
  3805.     }
  3806.       if (TREE_CODE (ftype) == POINTER_TYPE)
  3807.     {
  3808.       if (TREE_CODE (TREE_TYPE (ftype)) == FUNCTION_TYPE
  3809.           || TREE_CODE (TREE_TYPE (ftype)) == METHOD_TYPE)
  3810.         {
  3811.           /* This is a member which is a pointer to function.  */
  3812.           tree ref = build_component_ref_1 (build_indirect_ref (instance_ptr, 0, 0),
  3813.                         field, LOOKUP_COMPLAIN);
  3814.           if (ref == error_mark_node)
  3815.         return error_mark_node;
  3816.           return build_function_call (ref, parms);
  3817.         }
  3818.     }
  3819.       else if (TREE_CODE (ftype) == METHOD_TYPE)
  3820.     {
  3821.       error ("invalid call via pointer-to-member function");
  3822.       return error_mark_node;
  3823.     }
  3824.       else
  3825.     return NULL_TREE;
  3826.     }
  3827.   return NULL_TREE;
  3828. }
  3829.  
  3830. /* Build a method call of the form `EXP->SCOPES::NAME (PARMS)'.
  3831.    This is how virtual function calls are avoided.  */
  3832. tree
  3833. build_scoped_method_call (exp, scopes, name, parms)
  3834.      tree exp;
  3835.      tree scopes;
  3836.      tree name;
  3837.      tree parms;
  3838. {
  3839.   /* Because this syntactic form does not allow
  3840.      a pointer to a base class to be `stolen',
  3841.      we need not protect the drived->base conversion
  3842.      that happens here.
  3843.      
  3844.      @@ But we do have to check visibility privileges later.  */
  3845.   tree basename = (TREE_CODE (scopes) == SCOPE_REF) ? TREE_OPERAND (scopes, 1) : scopes;
  3846.   tree basetype, decl;
  3847.   tree type = TREE_TYPE (exp);
  3848.  
  3849.   if (type == error_mark_node
  3850.       || ! is_aggr_typedef (basename, 1))
  3851.     return error_mark_node;
  3852.  
  3853.   basetype = TREE_TYPE (TREE_TYPE (basename));
  3854.  
  3855.   if (basetype = basetype_or_else (basetype, type))
  3856.     {
  3857.       if (basetype == error_mark_node)
  3858.     return error_mark_node;
  3859.       if (TREE_CODE (exp) == INDIRECT_REF)
  3860.     decl = build_indirect_ref (convert_pointer_to (basetype,
  3861.                                build_unary_op (ADDR_EXPR, exp, 0)), 0);
  3862.       else
  3863.     decl = build_scoped_ref (exp, scopes);
  3864.  
  3865.       /* Call to a destructor.  */
  3866.       if (TREE_CODE (name) == BIT_NOT_EXPR)
  3867.     {
  3868.       /* Explicit call to destructor.  */
  3869.       name = TREE_OPERAND (name, 0);
  3870.       if (! is_aggr_typedef (name, 1))
  3871.         return error_mark_node;
  3872.       if (TREE_TYPE (decl) != TREE_TYPE (TREE_TYPE (name)))
  3873.         {
  3874.           error_with_aggr_type (TREE_TYPE (decl),
  3875.                     "qualified type `%s' does not match destructor type `%s'",
  3876.                     IDENTIFIER_POINTER (name));
  3877.           return error_mark_node;
  3878.         }
  3879.       return build_delete (TREE_TYPE (TREE_TYPE (name)),
  3880.                    decl, integer_one_node,
  3881.                    LOOKUP_NORMAL, 0);
  3882.     }
  3883.  
  3884.       /* Call to a method.  */
  3885.       return build_method_call (decl, name, parms, NULL_TREE,
  3886.                 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL);
  3887.     }
  3888.   return error_mark_node;
  3889. }
  3890.  
  3891. /* Build something of the form ptr->method (args)
  3892.    or object.method (args).  This can also build
  3893.    calls to constructors, and find friends.
  3894.  
  3895.    Member functions always take their class variable
  3896.    as a pointer.
  3897.  
  3898.    INSTANCE is a class instance.
  3899.  
  3900.    NAME is the NAME field of the struct, union, or class
  3901.    whose type is that of INSTANCE.
  3902.  
  3903.    PARMS help to figure out what that NAME really refers to.
  3904.  
  3905.    BASETYPE_PATH, if non-NULL, tells which basetypes of INSTANCE
  3906.    we should be traversed before starting our search.  We need
  3907.    this information to get protected accesses correct.
  3908.  
  3909.    FLAGS is the logical disjunction of zero or more LOOKUP_
  3910.    flags.  See cplus-tree.h for more info.
  3911.  
  3912.    If this is all OK, calls build_function_call with the resolved
  3913.    member function.
  3914.  
  3915.    This function must also handle being called to perform
  3916.    initialization, promotion/coercion of arguments, and
  3917.    instantiation of default parameters.
  3918.  
  3919.    Note that NAME may refer to an instance variable name.  If
  3920.    `operator()()' is defined for the type of that field, then we return
  3921.    that result.  */
  3922. tree
  3923. build_method_call (instance, name, parms, basetype_path, flags)
  3924.      tree instance, name, parms, basetype_path;
  3925.      int flags;
  3926. {
  3927.   register tree function, fntype, value_type;
  3928.   register tree basetype, save_basetype;
  3929.   register tree baselink, result, method_name, parmtypes, parm;
  3930.   tree last;
  3931.   int pass;
  3932.   enum visibility_type visibility;
  3933.   int rank_for_overload ();
  3934.   int need_vtbl = 0;
  3935.   char *err_name;
  3936.   char *name_kind;
  3937.   int ever_seen = 0;
  3938.   int wrap;
  3939.   tree wrap_type;
  3940.   tree instance_ptr = NULL_TREE;
  3941.   int all_virtual = flag_all_virtual;
  3942.   int static_call_context;
  3943.   tree saw_private = 0;
  3944.   tree saw_protected = 0;
  3945. #ifdef SOS
  3946.   /* If call is a call to a constructor, then `dtbl'
  3947.      will first be initialized with the function table pointer
  3948.      of the appropriate type (calling "sosFindCode" as a last
  3949.      resort), the the call to the constructor will go through there.  */
  3950.   tree dtbl = (flags & LOOKUP_DYNAMIC) ? TREE_VALUE (parms) : NULL_TREE;
  3951.  
  3952.   /* Flag saying whether or not `dtbl' has been inserted into the
  3953.      parameter list.  This is needed because we cannot tell (until
  3954.      we have a match) whether this parameter should go in or not.
  3955.  
  3956.      If 1, then `dtbl' is living naturally.
  3957.      If 0, then `dtbl' is not among the parms that we know about.
  3958.      If -1, the `dtbl' was place into the parms unnaturally.
  3959.  
  3960.      Note that we may side-effect the parameter list, but in such a way
  3961.      that the caller of this function would never know.  */
  3962.   int dtbl_inserted = (flags & LOOKUP_DYNAMIC);
  3963. #endif
  3964.  
  3965.   /* Keep track of `const' and `volatile' objects.  */
  3966.   int constp, volatilep;
  3967.  
  3968. #ifdef GATHER_STATISTICS
  3969.   n_build_method_call++;
  3970. #endif
  3971.  
  3972.   if (instance == error_mark_node
  3973.       || name == error_mark_node
  3974.       || parms == error_mark_node
  3975.       || (instance != 0 && TREE_TYPE (instance) == error_mark_node))
  3976.     return error_mark_node;
  3977.  
  3978.   if (TREE_CODE (name) == BIT_NOT_EXPR)
  3979.     {
  3980.       error ("invalid call to destructor, use qualified name `%s::~%s'",
  3981.          IDENTIFIER_POINTER (name), IDENTIFIER_POINTER (name));
  3982.       return error_mark_node;
  3983.     }
  3984.   if (TREE_CODE (name) == WRAPPER_EXPR)
  3985.     {
  3986.       wrap_type = TREE_OPERAND (name, 0);
  3987.       name = TREE_OPERAND (name, 1);
  3988.       wrap = 1;
  3989.     }
  3990.   else if (TREE_CODE (name) == ANTI_WRAPPER_EXPR)
  3991.     {
  3992.       wrap_type = TREE_OPERAND (name, 0);
  3993.       name = TREE_OPERAND (name, 1);
  3994.       wrap = -1;
  3995.     }
  3996.   else
  3997.     {
  3998.       wrap_type = NULL_TREE;
  3999.       wrap = 0;
  4000.     }
  4001.  
  4002.   /* Initialize name for error reporting.  */
  4003.   if (TREE_CODE (name) == OP_IDENTIFIER)
  4004.     name = build_operator_fnname (&name, parms, 1);
  4005.  
  4006.   if (OPERATOR_NAME_P (name))
  4007.     {
  4008.       char *p = operator_name_string (name);
  4009.       err_name = (char *)alloca (strlen (p) + 10);
  4010.       sprintf (err_name, "operator %s", p);
  4011.     }
  4012.   else if (name == wrapper_name)
  4013.     err_name = "wrapper";
  4014.   else if (OPERATOR_TYPENAME_P (name))
  4015.     err_name = "type conversion operator";
  4016.   else if (TREE_CODE (name) == SCOPE_REF)
  4017.     err_name = IDENTIFIER_POINTER (TREE_OPERAND (name, 1));
  4018.   else
  4019.     err_name = IDENTIFIER_POINTER (name);
  4020.  
  4021.   if (wrap)
  4022.     {
  4023.       char *p = (char *)alloca (strlen (err_name) + 32);
  4024.       sprintf (p, "%s for `%s'", wrap < 0 ? "anti-wrapper" : "wrapper", err_name);
  4025.       err_name = p;
  4026.     }
  4027.  
  4028.   if (instance == NULL_TREE)
  4029.     {
  4030.       static_call_context = 0;
  4031.  
  4032.       basetype = NULL_TREE;
  4033.       /* Check cases where this is really a call to raise
  4034.      an exception.  */
  4035.       if (current_class_type && TREE_CODE (name) == IDENTIFIER_NODE)
  4036.     {
  4037.       basetype = purpose_member (name, CLASSTYPE_TAGS (current_class_type));
  4038.       if (basetype)
  4039.         basetype = TREE_VALUE (basetype);
  4040.     }
  4041.       else if (TREE_CODE (name) == SCOPE_REF
  4042.            && TREE_CODE (TREE_OPERAND (name, 0)) == IDENTIFIER_NODE)
  4043.     {
  4044.       if (! is_aggr_typedef (TREE_OPERAND (name, 0), 1))
  4045.         return error_mark_node;
  4046.       basetype = purpose_member (TREE_OPERAND (name, 1),
  4047.                      CLASSTYPE_TAGS (TREE_TYPE (TREE_TYPE (TREE_OPERAND (name, 0)))));
  4048.       if (basetype)
  4049.         basetype = TREE_VALUE (basetype);
  4050.     }
  4051.  
  4052.       if (basetype != NULL_TREE)
  4053.     ;
  4054.       /* call to a constructor... */
  4055.       else if (TREE_TYPE (name))
  4056.     basetype = TREE_TYPE (TREE_TYPE (name));
  4057.       else
  4058.     {
  4059.       tree typedef_name = lookup_name (name);
  4060.       if (typedef_name && TREE_CODE (typedef_name) == TYPE_DECL)
  4061.         {
  4062.           /* Cannonicalize the typedef name.  */
  4063.           basetype = TREE_TYPE (typedef_name);
  4064.           name = DECL_NAME (TYPE_NAME (basetype));
  4065.         }
  4066.       else
  4067.         {
  4068.           error ("no constructor named `%s' in visible scope",
  4069.              IDENTIFIER_POINTER (name));
  4070.           return error_mark_node;
  4071.         }
  4072.     }
  4073.       if (wrap_type && wrap_type != basetype)
  4074.     {
  4075.       error_with_aggr_type (wrap_type, "invalid constructor `%s::%s'",
  4076.                 TYPE_NAME_STRING (basetype));
  4077.       return error_mark_node;
  4078.     }
  4079.       if (TYPE_VIRTUAL_P (basetype))
  4080.     {
  4081.       wrap_type = basetype;
  4082.     }
  4083.  
  4084.       if (! IS_AGGR_TYPE (basetype))
  4085.     {
  4086.     non_aggr_error:
  4087.       if ((flags & LOOKUP_COMPLAIN) && TREE_CODE (basetype) != ERROR_MARK)
  4088.         error ("request for member `%s' in something not a structure or union", err_name);
  4089.  
  4090.       return error_mark_node;
  4091.     }
  4092.     }
  4093.   else if (instance == C_C_D || instance == current_class_decl)
  4094.     {
  4095.       static_call_context = 0;
  4096.       instance = C_C_D;
  4097.       instance_ptr = current_class_decl;
  4098.       result = build_field_call (CLASSTYPE_AS_LIST (current_class_type),
  4099.                  instance_ptr, name, parms, err_name);
  4100.  
  4101.       if (result)
  4102.     return result;
  4103.  
  4104.       /* When doing initialization, we side-effect the TREE_TYPE of
  4105.      C_C_D, hence we cannot set up BASETYPE from CURRENT_CLASS_TYPE.  */
  4106.       basetype = TREE_TYPE (instance);
  4107.       if (!(flags & LOOKUP_NONVIRTUAL) && TYPE_VIRTUAL_P (basetype)
  4108.       && wrap_type == NULL_TREE)
  4109.     need_vtbl = 1;
  4110.     }
  4111.   else if (TREE_CODE (instance) == RESULT_DECL)
  4112.     {
  4113.       static_call_context = 0;
  4114.       basetype = TREE_TYPE (instance);
  4115.       if (wrap_type)
  4116.     {
  4117.       if (basetype_or_else (basetype, wrap_type))
  4118.         basetype = wrap_type;
  4119.       else
  4120.         return error_mark_node;
  4121.     }
  4122.       /* Should we ever have to make a virtual function reference
  4123.      from a RESULT_DECL, know that it must be of fixed type
  4124.      within the scope of this function.  */
  4125.       else if (!(flags & LOOKUP_NONVIRTUAL) && TYPE_VIRTUAL_P (basetype))
  4126.     need_vtbl = 1;
  4127.       instance_ptr = build1 (ADDR_EXPR, TYPE_POINTER_TO (basetype), instance);
  4128.     }
  4129.   else if (instance == current_exception_object)
  4130.     {
  4131.       instance_ptr = build1 (ADDR_EXPR, TYPE_POINTER_TO (current_exception_type),
  4132.                 TREE_OPERAND (current_exception_object, 0));
  4133.       mark_addressable (TREE_OPERAND (current_exception_object, 0));
  4134.       result = build_field_call (CLASSTYPE_AS_LIST (current_exception_type),
  4135.                  instance_ptr, name, parms, err_name);
  4136.       if (result)
  4137.     return result;
  4138.       error ("exception member `%s' cannot be invoked", err_name);
  4139.       return error_mark_node;
  4140.     }
  4141.   else
  4142.     {
  4143.       /* The MAIN_VARIANT of the type that `instance_ptr' winds up being.  */
  4144.       tree inst_ptr_basetype;
  4145.  
  4146.       /* from the file "cplus-typeck.c".  */
  4147.       extern tree unary_complex_lvalue ();
  4148.  
  4149.       static_call_context = (TREE_CODE (instance) == NOP_EXPR
  4150.                  && TREE_OPERAND (instance, 0) == error_mark_node);
  4151.  
  4152.       /* the base type of an instance variable is pointer to class */
  4153.       basetype = TREE_TYPE (instance);
  4154.  
  4155.       if (TREE_CODE (basetype) == REFERENCE_TYPE)
  4156.     {
  4157.       basetype = TYPE_MAIN_VARIANT (TREE_TYPE (basetype));
  4158.       if (! IS_AGGR_TYPE (basetype))
  4159.         goto non_aggr_error;
  4160.       /* Call to convert not needed because we are remaining
  4161.          within the same type.  */
  4162.       instance_ptr = build1 (NOP_EXPR, TYPE_POINTER_TO (basetype), instance);
  4163.       inst_ptr_basetype = basetype;
  4164.     }
  4165.       else
  4166.     {
  4167.       if (TREE_CODE (basetype) == POINTER_TYPE)
  4168.         {
  4169.           basetype = TREE_TYPE (basetype);
  4170.           instance_ptr = instance;
  4171.         }
  4172.  
  4173.       if (! IS_AGGR_TYPE (basetype))
  4174.         goto non_aggr_error;
  4175.  
  4176.       if (! instance_ptr)
  4177.         {
  4178.           if ((lvalue_p (instance)
  4179.            && (instance_ptr = build_unary_op (ADDR_EXPR, instance, 0)))
  4180.           || (instance_ptr = unary_complex_lvalue (ADDR_EXPR, instance)))
  4181.         {
  4182.           if (instance_ptr == error_mark_node)
  4183.             return error_mark_node;
  4184.         }
  4185.           else if (TREE_CODE (instance) == NOP_EXPR
  4186.                || TREE_CODE (instance) == CONSTRUCTOR)
  4187.         {
  4188.           /* A cast is not an lvalue.  Initialize a fresh temp
  4189.              with the value we are casting from, and proceed with
  4190.              that temporary.  We can't cast to a reference type,
  4191.              so that simplifies the initialization to something
  4192.              we can manage.  */
  4193.           tree temp = get_temp_name (TREE_TYPE (instance), 0);
  4194.           if (IS_AGGR_TYPE (TREE_TYPE (instance)))
  4195.             expand_aggr_init (temp, instance, 0);
  4196.           else
  4197.             {
  4198.               store_init_value (temp, instance);
  4199.               expand_decl_init (temp);
  4200.             }
  4201.           instance = temp;
  4202.           instance_ptr = build_unary_op (ADDR_EXPR, instance, 0);
  4203.         }
  4204.           else
  4205.         {
  4206.           assert (TREE_CODE (instance) == CALL_EXPR);
  4207.           if (TYPE_NEEDS_CONSTRUCTOR (basetype))
  4208.             instance = build_cplus_new (basetype, instance);
  4209.           else
  4210.             {
  4211.               instance = get_temp_name (basetype, 0);
  4212.               TREE_ADDRESSABLE (instance) = 1;
  4213.             }
  4214.           instance_ptr = build_unary_op (ADDR_EXPR, instance, 0);
  4215.         }
  4216.           /* @@ Should we call comp_target_types here?  */
  4217.           inst_ptr_basetype = TREE_TYPE (TREE_TYPE (instance_ptr));
  4218.           if (TYPE_MAIN_VARIANT (basetype) == TYPE_MAIN_VARIANT (inst_ptr_basetype))
  4219.         basetype = inst_ptr_basetype;
  4220.           else
  4221.         instance_ptr = convert (TYPE_POINTER_TO (basetype), instance_ptr);
  4222.         }
  4223.       else
  4224.         inst_ptr_basetype = TREE_TYPE (TREE_TYPE (instance_ptr));
  4225.     }
  4226.  
  4227.       if (basetype_path == NULL_TREE)
  4228.     basetype_path = CLASSTYPE_AS_LIST (inst_ptr_basetype);
  4229.  
  4230.       result = build_field_call (basetype_path, instance_ptr, name, parms, err_name);
  4231.       if (result)
  4232.     return result;
  4233.  
  4234.       if (wrap_type)
  4235.     {
  4236.       if (basetype_or_else (basetype, wrap_type))
  4237.         basetype = wrap_type;
  4238.       else
  4239.         return error_mark_node;
  4240.     }
  4241.       else if (!(flags & LOOKUP_NONVIRTUAL) && TYPE_VIRTUAL_P (basetype))
  4242.     {
  4243.       if (TREE_VOLATILE (instance_ptr))
  4244.         {
  4245.           /* This action is needed because the instance is needed
  4246.          for providing the base of the virtual function table.
  4247.          Without using a SAVE_EXPR, the function we are building
  4248.          may be called twice, or side effects on the instance
  4249.          variable (such as a post-increment), may happen twice.  */
  4250.           instance_ptr = save_expr (instance_ptr);
  4251.           instance = build_indirect_ref (instance_ptr, 0);
  4252.         }
  4253.       else if (TREE_CODE (TREE_TYPE (instance)) == POINTER_TYPE)
  4254.         {
  4255.           /* This happens when called for operator new ().  */
  4256.           instance = build_indirect_ref (instance, 0);
  4257.         }
  4258.  
  4259.       need_vtbl = 1;
  4260.     }
  4261.     }
  4262.  
  4263.   if (TYPE_SIZE (basetype) == 0)
  4264.     {
  4265.       /* This is worth complaining about, I think.  */
  4266.       error_with_aggr_type (basetype, "cannot lookup method in incomplete type `%s'");
  4267.       return error_mark_node;
  4268.     }
  4269.  
  4270.   /* Are we building a non-virtual wrapper?  */
  4271.   if (flags & LOOKUP_NONVIRTUAL)
  4272.     {
  4273.       if (all_virtual)
  4274.     sorry ("non-virtual call with -fall-virtual");
  4275.       if (wrap)
  4276.     wrap_type = basetype;
  4277.     }
  4278.  
  4279.   save_basetype = basetype;
  4280.  
  4281.   if (all_virtual == 1
  4282.       && (! strncmp (IDENTIFIER_POINTER (name), OPERATOR_METHOD_FORMAT,
  4283.              OPERATOR_METHOD_LENGTH)
  4284.       || instance_ptr == NULL_TREE
  4285.       || (TYPE_OVERLOADS_METHOD_CALL_EXPR (basetype) == 0
  4286.           && TYPE_NEEDS_WRAPPER (basetype) == 0)))
  4287.     all_virtual = 0;
  4288.  
  4289.   last = NULL_TREE;
  4290.   for (parmtypes = 0, parm = parms; parm; parm = TREE_CHAIN (parm))
  4291.     {
  4292.       tree t = TREE_TYPE (TREE_VALUE (parm));
  4293.       if (TREE_CODE (t) == OFFSET_TYPE)
  4294.     {
  4295.       /* Convert OFFSET_TYPE entities to their normal selves.  */
  4296.       TREE_VALUE (parm) = resolve_offset_ref (TREE_VALUE (parm));
  4297.       t = TREE_TYPE (TREE_VALUE (parm));
  4298.     }
  4299.       if (TREE_CODE (t) == ARRAY_TYPE)
  4300.     {
  4301.       /* Perform the conversion from ARRAY_TYPE to POINTER_TYPE in place.
  4302.          This eliminates needless calls to `compute_conversion_costs'.  */
  4303.       TREE_VALUE (parm) = default_conversion (TREE_VALUE (parm));
  4304.       t = TREE_TYPE (TREE_VALUE (parm));
  4305.     }
  4306.       if (t == error_mark_node)
  4307.     return error_mark_node;
  4308.       last = build_tree_list (NULL_TREE, t);
  4309.       parmtypes = chainon (parmtypes, last);
  4310.     }
  4311.  
  4312.   if (instance)
  4313.     {
  4314.       constp = TREE_READONLY (instance);
  4315.       volatilep = TREE_THIS_VOLATILE (instance);
  4316.       parms = tree_cons (NULL_TREE, instance_ptr, parms);
  4317.     }
  4318.   else
  4319.     {
  4320.       /* Raw constructors are always in charge.  */
  4321.       if (TYPE_USES_VIRTUAL_BASECLASSES (basetype)
  4322.       && ! (flags & LOOKUP_HAS_IN_CHARGE))
  4323.     {
  4324.       flags |= LOOKUP_HAS_IN_CHARGE;
  4325.       parms = tree_cons (NULL_TREE, integer_one_node, parms);
  4326.       parmtypes = tree_cons (NULL_TREE, integer_type_node, parmtypes);
  4327.     }
  4328.  
  4329.       if (flag_this_is_variable)
  4330.     {
  4331.       constp = 0;
  4332.       volatilep = 0;
  4333.       parms = tree_cons (NULL_TREE, build1 (NOP_EXPR, TYPE_POINTER_TO (basetype), integer_zero_node), parms);
  4334.     }
  4335.       else
  4336.     {
  4337.       constp = 0;
  4338.       volatilep = 0;
  4339.       instance_ptr = save_expr (build_new (NULL_TREE, basetype, void_type_node));
  4340.       TREE_CALLS_NEW (instance_ptr) = 1;
  4341.       instance = build_indirect_ref (instance_ptr, 0);
  4342.       parms = tree_cons (NULL_TREE, instance_ptr, parms);
  4343.     }
  4344.     }
  4345.   parmtypes = tree_cons (NULL_TREE,
  4346.              build_pointer_type (build_type_variant (basetype, constp, volatilep)),
  4347.              parmtypes);
  4348.   if (last == NULL_TREE)
  4349.     last = parmtypes;
  4350.  
  4351.   /* Look up function name in the structure type definition.  */
  4352.  
  4353.   if (wrap)
  4354.     {
  4355.       if (wrap > 0)
  4356.     name_kind = "wrapper";
  4357.       else
  4358.     name_kind = "anti-wrapper";
  4359.       baselink = get_wrapper (basetype);
  4360.     }
  4361.   else
  4362.     {
  4363.       if (TREE_TYPE (name)
  4364.       && TREE_CODE (TREE_TYPE (name)) == TYPE_DECL
  4365.       && IS_AGGR_TYPE (TREE_TYPE (TREE_TYPE (name))))
  4366.     {
  4367.       tree tmp = NULL_TREE;
  4368.       if (TREE_TYPE (name) == TYPE_NAME (basetype))
  4369.         tmp = basetype;
  4370.       else
  4371.         tmp = get_base_type (TREE_TYPE (TREE_TYPE (name)), basetype, 0);
  4372.       if (tmp != 0)
  4373.         {
  4374.           name_kind = "constructor";
  4375.  
  4376.           if (TYPE_USES_VIRTUAL_BASECLASSES (basetype)
  4377.             && ! (flags & LOOKUP_HAS_IN_CHARGE))
  4378.         {
  4379.           /* Constructors called for initialization
  4380.              only are never in charge.  */
  4381.           tree tmplist;
  4382.  
  4383.           flags |= LOOKUP_HAS_IN_CHARGE;
  4384.           tmplist = tree_cons (NULL_TREE, integer_zero_node,
  4385.                        TREE_CHAIN (parms));
  4386.           TREE_CHAIN (parms) = tmplist;
  4387.           tmplist = tree_cons (NULL_TREE, integer_type_node, TREE_CHAIN (parmtypes));
  4388.           TREE_CHAIN (parmtypes) = tmplist;
  4389.         }
  4390.  
  4391. #ifdef SOS
  4392.           if (TYPE_DYNAMIC (basetype) && dtbl_inserted == 0)
  4393.         {
  4394.           tree parm, parmtype;
  4395.           dtbl = get_sos_dtable (basetype);
  4396.           parm = tree_cons (NULL_TREE, dtbl, TREE_CHAIN (parms));
  4397.           parmtype = tree_cons (NULL_TREE, build_pointer_type (ptr_type_node), TREE_CHAIN (parmtypes));
  4398.           TREE_CHAIN (parms) = parm;
  4399.           TREE_CHAIN (parmtypes) = parmtype;
  4400.           dtbl_inserted = -1;
  4401.         }
  4402. #endif
  4403.           /* constructors are in very specific places.  */
  4404. #ifdef SOS
  4405.           if (dtbl_inserted == -1)
  4406.         {
  4407.           TREE_CHAIN (parmtypes) = TREE_CHAIN (TREE_CHAIN (parmtypes));
  4408.           TREE_CHAIN (parms) = TREE_CHAIN (TREE_CHAIN (parms));
  4409.           dtbl_inserted = 0;
  4410.         }
  4411. #endif
  4412.           basetype = tmp;
  4413.         }
  4414.       else
  4415.         name_kind = "method";
  4416.     }
  4417.       else name_kind = "method";
  4418.  
  4419.       if (basetype_path == NULL_TREE)
  4420.     basetype_path = CLASSTYPE_AS_LIST (basetype);
  4421.       result = lookup_fnfields (basetype_path, name,
  4422.                 (flags & LOOKUP_COMPLAIN));
  4423.       if (result == error_mark_node)
  4424.     return error_mark_node;
  4425.     }
  4426.  
  4427.   /* Now, go look for this method name. We do not find destructors here.
  4428.  
  4429.      Putting `void_list_node' on the end of the parmtypes
  4430.      fakes out `build_decl_overload' into doing the right thing.  */
  4431.   TREE_CHAIN (last) = void_list_node;
  4432.   method_name = build_decl_overload (IDENTIFIER_POINTER (name),
  4433.                      parmtypes,
  4434.                      1 + (name == DECL_NAME (TYPE_NAME (save_basetype))));
  4435.   TREE_CHAIN (last) = NULL_TREE;
  4436.  
  4437.   for (pass = 0; pass < 2; pass++)
  4438.     {
  4439.       struct candidate *candidates;
  4440.       struct candidate *cp;
  4441.       int len, best = 2;
  4442.  
  4443.       /* This increments every time we go up the type hierarchy.
  4444.      The idea is to prefer a function of the derived class if possible.  */
  4445.       int b_or_d;
  4446.  
  4447.       baselink = result;
  4448.  
  4449.       if (pass > 0)
  4450.     {
  4451.       candidates = (struct candidate *) alloca ((ever_seen+1) * sizeof (struct candidate));
  4452.       cp = candidates;
  4453.       len = list_length (parms);
  4454.       b_or_d = 0;
  4455.  
  4456.       /* First see if a global function has a shot at it.  */
  4457.       if (flags & LOOKUP_GLOBAL)
  4458.         {
  4459.           tree friend_parms;
  4460.           tree parm = TREE_VALUE (parms);
  4461.  
  4462.           if (TREE_CODE (TREE_TYPE (parm)) == REFERENCE_TYPE)
  4463.         friend_parms = parms;
  4464.           else if (TREE_CODE (TREE_TYPE (parm)) == POINTER_TYPE)
  4465.         {
  4466.           parm = build_indirect_ref (parm, "friendifying parms (compiler error)");
  4467.           parm = convert (build_reference_type (TREE_TYPE (parm)), parm);
  4468.           friend_parms = tree_cons (NULL_TREE, parm, TREE_CHAIN (parms));
  4469.         }
  4470.           else
  4471.         assert (0);
  4472.  
  4473.           cp->harshness
  4474.         = (unsigned short *)alloca ((len+1) * sizeof (short));
  4475.           result = build_overload_call (name, friend_parms, 0, cp);
  4476.           /* If it turns out to be the one we were actually looking for
  4477.          (it was probably a friend function), the return the
  4478.          good result.  */
  4479.           if (TREE_CODE (result) == CALL_EXPR)
  4480.         return result;
  4481.  
  4482.           while (cp->evil == 0)
  4483.         {
  4484.           /* non-standard uses: set the field to 0 to indicate
  4485.              we are using a non-member function.  */
  4486.           cp->u.field = 0;
  4487.           if (cp->harshness[len] == 0
  4488.               && cp->harshness[len] == 0
  4489.               && cp->user == 0 && cp->b_or_d == 0
  4490.               && cp->easy < best)
  4491.             best = cp->easy;
  4492.           cp += 1;
  4493.         }
  4494.         }
  4495.     }
  4496.  
  4497.       while (baselink)
  4498.     {
  4499.       /* We have a hit (of sorts). If the parameter list is
  4500.          "error_mark_node", or some variant thereof, it won't
  4501.          match any methods. Since we have verified that the is
  4502.          some method vaguely matching this one (in name at least),
  4503.          silently return.
  4504.          
  4505.          Don't stop for friends, however.  */
  4506.       tree basetypes = TREE_PURPOSE (baselink);
  4507.  
  4508.       function = TREE_VALUE (baselink);
  4509.       basetype = TREE_VALUE (basetypes);
  4510.  
  4511.       /* Cast the instance variable to the approriate type.  */
  4512.       TREE_VALUE (parmtypes) = TYPE_POINTER_TO (basetype);
  4513.  
  4514.       if (DESTRUCTOR_NAME_P (DECL_NAME (function)))
  4515.         function = TREE_CHAIN (function);
  4516.  
  4517.       for (; function; function = TREE_CHAIN (function))
  4518.         {
  4519. #ifdef GATHER_STATISTICS
  4520.           n_inner_fields_searched++;
  4521. #endif
  4522.           ever_seen++;
  4523.  
  4524.           /* Not looking for friends here.  */
  4525.           if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE
  4526.           && ! DECL_STATIC_FUNCTION_P (function))
  4527.         continue;
  4528.  
  4529.           if (pass == 0
  4530.           && DECL_NAME (function) == method_name)
  4531.         {
  4532.           if (flags & LOOKUP_PROTECT)
  4533.             {
  4534.               visibility = compute_visibility (basetypes, function);
  4535.               if (visibility == visibility_protected
  4536.               && flags & LOOKUP_PROTECTED_OK)
  4537.             visibility = visibility_public;
  4538.             }
  4539.  
  4540.           if ((flags & LOOKUP_PROTECT) == 0
  4541.               || visibility == visibility_public)
  4542.             goto found_and_ok;
  4543.           else if (visibility == visibility_private)
  4544.             saw_private = function;
  4545.           else if (visibility == visibility_protected)
  4546.             saw_protected = function;
  4547.           /* If we fail on the exact match, we have
  4548.              an immediate failure.  */
  4549.           goto found;
  4550.         }
  4551.           if (pass > 0)
  4552.         {
  4553.           tree these_parms = parms;
  4554.  
  4555. #ifdef GATHER_STATISTICS
  4556.           n_inner_fields_searched++;
  4557. #endif
  4558.           cp->harshness
  4559.             = (unsigned short *)alloca ((len+1) * sizeof (short));
  4560.           if (DECL_STATIC_FUNCTION_P (function))
  4561.             these_parms = TREE_CHAIN (these_parms);
  4562.           compute_conversion_costs (function, these_parms, cp, len);
  4563.           cp->b_or_d += b_or_d;
  4564.           if (cp->evil == 0)
  4565.             {
  4566.               cp->u.field = function;
  4567.               cp->function = function;
  4568.               if (flags & LOOKUP_PROTECT)
  4569.             {
  4570.               enum visibility_type this_v;
  4571.               this_v = compute_visibility (basetypes, function);
  4572.               if (this_v == visibility_protected
  4573.                   && (flags & LOOKUP_PROTECTED_OK))
  4574.                 this_v = visibility_public;
  4575.               if (this_v != visibility_public)
  4576.                 {
  4577.                   if (this_v == visibility_private)
  4578.                 saw_private = function;
  4579.                   else
  4580.                 saw_protected = function;
  4581.                   continue;
  4582.                 }
  4583.             }
  4584.  
  4585.               /* No "two-level" conversions.  */
  4586.               if (flags & LOOKUP_NO_CONVERSION && cp->user != 0)
  4587.             continue;
  4588.  
  4589.               /* If we used default parameters, we must
  4590.              check to see whether anyone else might
  4591.              use them also, and report a possible
  4592.              ambiguity.  */
  4593.               if (! TYPE_USES_MULTIPLE_INHERITANCE (save_basetype)
  4594.               && cp->harshness[len] == 0
  4595.               && (cp->harshness[0] & 128) == 0
  4596.               && cp->user == 0 && cp->b_or_d == 0
  4597.               && cp->easy < best)
  4598.             {
  4599.               if (! DECL_STATIC_FUNCTION_P (function))
  4600.                 TREE_VALUE (parms) = cp->arg;
  4601.               if (best == 2)
  4602.                 goto found_and_maybe_warn;
  4603.             }
  4604.               cp++;
  4605.             }
  4606.         }
  4607.         }
  4608.       /* Now we have run through one link's member functions.
  4609.          arrange to head-insert this link's links.  */
  4610.       baselink = next_baselink (baselink);
  4611.       b_or_d += 1;
  4612.     }
  4613.       if (pass == 0)
  4614.     {
  4615.       /* No exact match could be found.  Now try to find match
  4616.          using default conversions.  */
  4617.       if ((flags & LOOKUP_GLOBAL) && IDENTIFIER_GLOBAL_VALUE (name))
  4618.         if (TREE_CODE (IDENTIFIER_GLOBAL_VALUE (name)) == FUNCTION_DECL)
  4619.           ever_seen += 1;
  4620.         else if (TREE_CODE (IDENTIFIER_GLOBAL_VALUE (name)) == TREE_LIST)
  4621.           ever_seen += list_length (IDENTIFIER_GLOBAL_VALUE (name));
  4622.  
  4623.       if (ever_seen == 0)
  4624.         {
  4625.           if (flags & LOOKUP_GLOBAL)
  4626.         error ("no global or member function `%s' defined", err_name);
  4627.           else
  4628.         error_with_aggr_type (save_basetype, "no member function `%s::%s'", err_name);
  4629.           return error_mark_node;
  4630.         }
  4631.       continue;
  4632.     }
  4633.  
  4634.       if (cp - candidates != 0)
  4635.     {
  4636.       /* Rank from worst to best.  Then cp will point to best one.
  4637.          Private fields have their bits flipped.  For unsigned
  4638.          numbers, this should make them look very large.
  4639.          If the best alternate has a (signed) negative value,
  4640.          then all we ever saw were private members.  */
  4641.       if (cp - candidates > 1)
  4642.         {
  4643.           cp = ideal_candidate (save_basetype, candidates,
  4644.                     cp - candidates, parms, len);
  4645.           if (cp == 0)
  4646.         {
  4647.           error ("ambiguous type conversion requested for %s `%s'",
  4648.              name_kind, err_name);
  4649.           return error_mark_node;
  4650.         }
  4651.         }
  4652.       else if (cp[-1].evil == 2)
  4653.         {
  4654.           error ("ambiguous type conversion requested for %s `%s'",
  4655.              name_kind, err_name);
  4656.           return error_mark_node;
  4657.         }
  4658.       else cp--;
  4659.  
  4660.       /* The global function was the best, so use it.  */
  4661.       if (cp->u.field == 0)
  4662.         {
  4663.           /* We must convert the instance pointer into a reference type.
  4664.          Global overloaded functions can only either take
  4665.          aggregate objects (which come for free from references)
  4666.          or reference data types anyway.  */
  4667.           TREE_VALUE (parms) = copy_node (instance_ptr);
  4668.           TREE_TYPE (TREE_VALUE (parms)) = build_reference_type (TREE_TYPE (TREE_TYPE (instance_ptr)));
  4669.           return build_function_call (cp->function, parms);
  4670.         }
  4671.  
  4672.       function = cp->function;
  4673.       if (DECL_STATIC_FUNCTION_P (function))
  4674.         basetype = NULL_TREE;
  4675.       else
  4676.         {
  4677.           basetype = TREE_TYPE (TREE_TYPE (cp->arg));
  4678.           TREE_VALUE (parms) = cp->arg;
  4679.         }
  4680.       goto found_and_maybe_warn;
  4681.     }
  4682.  
  4683.       if ((flags & ~LOOKUP_GLOBAL) & (LOOKUP_COMPLAIN|LOOKUP_SPECULATIVELY))
  4684.     {
  4685.       char *tag_name, *buf;
  4686.  
  4687.       if (DECL_STATIC_FUNCTION_P (cp->function))
  4688.         parms = TREE_CHAIN (parms);
  4689.       if (ever_seen)
  4690.         {
  4691.           if (((int)saw_protected|(int)saw_private) == 0)
  4692.         {
  4693.           if (flags & LOOKUP_SPECULATIVELY)
  4694.             return NULL_TREE;
  4695.           if (static_call_context && TREE_CODE (TREE_TYPE (cp->function)) == METHOD_TYPE)
  4696.             error_with_aggr_type (TREE_TYPE (TREE_TYPE (instance_ptr)),
  4697.                       "object missing in call to `%s::%s'",
  4698.                       err_name);
  4699.           else
  4700.             report_type_mismatch (cp, parms, name_kind, err_name);
  4701.         }
  4702.           else
  4703.         {
  4704.           char buf[80];
  4705.           char *msg;
  4706.           tree seen = saw_private;
  4707.  
  4708.           if (saw_private)
  4709.             if (saw_protected)
  4710.               msg = "%s %%s (and the like) are private or protected";
  4711.             else
  4712.               msg = "the %s %%s is private";
  4713.           else
  4714.             {
  4715.               msg = "the %s %%s is protected";
  4716.               seen = saw_protected;
  4717.             }
  4718.           sprintf (buf, msg, name_kind);
  4719.           error_with_decl (seen, buf);
  4720.           error ("within this context");
  4721.         }
  4722.           return error_mark_node;
  4723.         }
  4724.  
  4725.       if ((flags & ~LOOKUP_SPECULATIVELY) & LOOKUP_COMPLAIN)
  4726.         {
  4727.           if (TREE_CODE (save_basetype) == RECORD_TYPE)
  4728.         tag_name = "structure";
  4729.           else
  4730.         tag_name = "union";
  4731.  
  4732.           if (wrap)
  4733.         buf = "%s has no appropriate wrapper function defined";
  4734.           else
  4735.         {
  4736.           buf = (char *)alloca (30 + strlen (err_name));
  4737.           strcpy (buf, "%s has no method named `%s'");
  4738.         }
  4739.  
  4740.           error (buf, tag_name, err_name);
  4741.           return error_mark_node;
  4742.         }
  4743.       return NULL_TREE;
  4744.     }
  4745.       continue;
  4746.  
  4747.     found_and_maybe_warn:
  4748.       if (cp->harshness[0] & 128)
  4749.     {
  4750.       if (flags & LOOKUP_COMPLAIN)
  4751.         {
  4752.           error_with_decl (cp->function, "non-const member function `%s'");
  4753.           error ("called for const object at this point in file");
  4754.         }
  4755.       /* Not good enough for a match.  */
  4756.       else return error_mark_node;
  4757.     }
  4758.       goto found_and_ok;
  4759.     }
  4760.   /* Silently return error_mark_node.  */
  4761.   return error_mark_node;
  4762.  
  4763.  found:
  4764.   if (visibility == visibility_private)
  4765.     {
  4766.       if (flags & LOOKUP_COMPLAIN)
  4767.     error (TREE_PRIVATE (function)
  4768.            ? "%s `%s' is private"
  4769.            : "%s `%s' is from private base class",
  4770.            name_kind,
  4771.            lang_printable_name (function));
  4772.       return error_mark_node;
  4773.     }
  4774.   else if (visibility == visibility_protected)
  4775.     {
  4776.       if (flags & LOOKUP_COMPLAIN)
  4777.     error (TREE_PROTECTED (function)
  4778.            ? "%s `%s' is protected"
  4779.            : "%s `%s' has protected visibility from this point",
  4780.            name_kind,
  4781.            lang_printable_name (function));
  4782.       return error_mark_node;
  4783.     }
  4784.   abort ();
  4785.  
  4786.  found_and_ok:
  4787.  
  4788.   /* From here on down, BASETYPE is the type that INSTANCE_PTR's
  4789.      type (if it exists) is a pointer to.  */
  4790.   basetype = DECL_CONTEXT (function);
  4791.   fntype = TREE_TYPE (function);
  4792.  
  4793.   if (TREE_CODE (fntype) == POINTER_TYPE)
  4794.     fntype = TREE_TYPE (fntype);
  4795.  
  4796.   if (TREE_CODE (fntype) == METHOD_TYPE && static_call_context)
  4797.     {
  4798.       /* Let's be nice to the user for now, and give reasonable
  4799.      default behavior.  */
  4800.       instance_ptr = current_class_decl;
  4801.       if (instance_ptr)
  4802.     {
  4803.       if (basetype != current_class_type)
  4804.         {
  4805.           basetype = get_base_type (basetype, current_class_type, 1);
  4806.           if (basetype == 0)
  4807.         {
  4808.           error_not_base_type (DECL_CONTEXT (function), current_class_type);
  4809.           return error_mark_node;
  4810.         }
  4811.           else if (basetype == error_mark_node)
  4812.         return error_mark_node;
  4813.         }
  4814.     }
  4815.       else
  4816.     {
  4817.       error_with_aggr_type (basetype, "cannot call member function `%s::%s' without object",
  4818.                 err_name);
  4819.       return error_mark_node;
  4820.     }
  4821.     }
  4822.  
  4823.   value_type = TREE_TYPE (fntype) ? TREE_TYPE (fntype) : void_type_node;
  4824.  
  4825.   if (TYPE_SIZE (value_type) == 0)
  4826.     {
  4827.       if (flags & LOOKUP_COMPLAIN)
  4828.     incomplete_type_error (0, value_type);
  4829.       return error_mark_node;
  4830.     }
  4831.  
  4832.   /* We do not pass FUNCTION into `actualparameterlist', because by
  4833.      now everything should be ok.  If not, then we have a serious error.  */
  4834.   if (DECL_STATIC_FUNCTION_P (function))
  4835.     parms = actualparameterlist (NULL_TREE, TYPE_ARG_TYPES (fntype),
  4836.                  TREE_CHAIN (parms), NULL_TREE, LOOKUP_NORMAL);
  4837.   else
  4838.     {
  4839.       int sub_flags = DECL_CONSTRUCTOR_P (function) ? flags : LOOKUP_NORMAL;
  4840.  
  4841.       if ((flags & LOOKUP_NONVIRTUAL) == 0)
  4842.     basetype = DECL_VCONTEXT (function);
  4843.  
  4844.       /* First parm could be integer_zerop with casts like
  4845.      ((Object*)0)->Object::IsA()  */
  4846.       if (!integer_zerop (TREE_VALUE (parms)))
  4847.     {
  4848.       instance_ptr = convert_pointer_to (build_type_variant (basetype, constp, volatilep),
  4849.                          TREE_VALUE (parms));
  4850.       if (TREE_CODE (instance_ptr) == COND_EXPR)
  4851.         {
  4852.           instance_ptr = save_expr (instance_ptr);
  4853.           instance = build_indirect_ref (instance_ptr);
  4854.         }
  4855.       else if (TREE_CODE (instance_ptr) == NOP_EXPR
  4856.            && TREE_CODE (TREE_OPERAND (instance_ptr, 0)) == ADDR_EXPR
  4857.            && TREE_OPERAND (TREE_OPERAND (instance_ptr, 0), 0) == instance)
  4858.         ;
  4859.       else if (instance == NULL_TREE
  4860.            || TREE_CODE (instance) != INDIRECT_REF
  4861.            || TREE_OPERAND (instance, 0) != instance_ptr)
  4862.         instance = build_indirect_ref (instance_ptr);
  4863.     }
  4864.       parms = tree_cons (NULL_TREE, instance_ptr,
  4865.              actualparameterlist (NULL_TREE, TREE_CHAIN (TYPE_ARG_TYPES (fntype)), TREE_CHAIN (parms), NULL_TREE, sub_flags));
  4866.     }
  4867.  
  4868.   /* See if there is a wrapper for this thing.  */
  4869.   if (wrap < 0
  4870.       || static_call_context
  4871.       || name == wrapper_name
  4872.       || name == DECL_NAME (TYPE_NAME (basetype)))
  4873.     ;
  4874.   else if (wrap > 0 || TYPE_NEEDS_WRAPPER (basetype))
  4875.     {
  4876.       flags &= ~LOOKUP_PROTECT;
  4877.       if (wrap == 0)
  4878.     {
  4879.       wrap = TYPE_NEEDS_WRAPPER (basetype);
  4880.       /* If no wrapper specified, wrapper may be virtual.  */
  4881.       flags &= ~LOOKUP_NONVIRTUAL;
  4882.     }
  4883.  
  4884.       if (wrap)
  4885.     {
  4886.       tree wrapped_result, unwrapped_result;
  4887.       register int bytecount = get_arglist_len_in_bytes (parms);
  4888.  
  4889.       if (!all_virtual && TREE_CODE (function) == FUNCTION_DECL)
  4890.         parm = build_unary_op (ADDR_EXPR, function, 0);
  4891.       else
  4892.         {
  4893.               fntype = build_cplus_method_type (basetype, TREE_TYPE (fntype), TYPE_ARG_TYPES (fntype));
  4894.           parm = build1 (NOP_EXPR, build_pointer_type (fntype), DECL_VINDEX (function));
  4895.         }
  4896.  
  4897.       if (TYPE_HAS_WRAPPER_PRED (basetype))
  4898.         {
  4899.           unwrapped_result = build_nt (CALL_EXPR, default_conversion (function), parms, NULL_TREE);
  4900.  
  4901.           assert (TREE_OPERAND (unwrapped_result, 1) != error_mark_node);
  4902.  
  4903.           TREE_TYPE (unwrapped_result) = value_type;
  4904.           TREE_VOLATILE (unwrapped_result) = 1;
  4905.           TREE_RAISES (unwrapped_result) = !! TYPE_RAISES_EXCEPTIONS (fntype);
  4906.         }
  4907.  
  4908.       /* If this pointer walked as a result of multiple inheritance,
  4909.          keep its displaced value.  */
  4910.       parms = tree_cons (NULL_TREE, build_int_2 (bytecount, 0),
  4911.                  tree_cons (NULL_TREE, parm, TREE_CHAIN (parms)));
  4912.  
  4913.       wrapped_result = get_wrapper (basetype);
  4914.       assert (wrapped_result != NULL_TREE);
  4915.       assert (wrapped_result != error_mark_node);
  4916.  
  4917.       /* @@ Should BASETYPE_PATH get TREE_PURPOSE (wrapped_result) here?  */
  4918.       wrapped_result
  4919.         = build_method_call (instance,
  4920.                  DECL_ORIGINAL_NAME (TREE_VALUE (wrapped_result)),
  4921.                  parms, basetype_path, flags);
  4922. #if 0
  4923.       /* Do this if we want the result of operator->() to inherit
  4924.          the type of the function it is subbing for.  */
  4925.       if (wrapped_result != error_mark_node)
  4926.         TREE_TYPE (wrapped_result) = value_type;
  4927. #endif
  4928.  
  4929.       if (TYPE_HAS_WRAPPER_PRED (basetype))
  4930.         {
  4931.           result = build_conditional_expr
  4932.         (build_method_call (instance, wrapper_pred_name, build_tree_list (NULL_TREE, parm), basetype_path, LOOKUP_NORMAL),
  4933.          wrapped_result,
  4934.          unwrapped_result);
  4935.  
  4936.         }
  4937.       else
  4938.         {
  4939.           result = wrapped_result;
  4940.         }
  4941.  
  4942.       TREE_VOLATILE (result) = 1;
  4943.       return result;
  4944.     }
  4945.     }
  4946.   /* Constructors do not overload method calls.  */
  4947.   else if (TYPE_OVERLOADS_METHOD_CALL_EXPR (basetype)
  4948.        && name != DECL_NAME (TYPE_NAME (basetype))
  4949.        && (TREE_CODE (function) != FUNCTION_DECL
  4950.            || strncmp (IDENTIFIER_POINTER (DECL_NAME (function)),
  4951.                OPERATOR_METHOD_FORMAT,
  4952.                OPERATOR_METHOD_LENGTH))
  4953.        && (may_be_remote (basetype)
  4954.            || (C_C_D ? TREE_TYPE (instance) != current_class_type : 1)))
  4955.     {
  4956. #ifdef ESKIT
  4957.       register int bytecount = 0;
  4958. #else
  4959.       register int bytecount = get_arglist_len_in_bytes (parms);
  4960. #endif
  4961.       tree fn_as_int;
  4962.  
  4963.       parms = tree_cons (NULL_TREE, build_int_2 (bytecount, 0),
  4964.              TREE_CHAIN (parms));
  4965.  
  4966.       if (!all_virtual && TREE_CODE (function) == FUNCTION_DECL)
  4967.     fn_as_int = build_unary_op (ADDR_EXPR, function, 0);
  4968.       else
  4969.     fn_as_int = convert (TREE_TYPE (default_conversion (function)), DECL_VINDEX (function));
  4970.       if (all_virtual == 1)
  4971.     fn_as_int = convert (integer_type_node, fn_as_int);
  4972.  
  4973.       result = build_opfncall (METHOD_CALL_EXPR, instance, fn_as_int, parms);
  4974.  
  4975.       if (result == NULL_TREE)
  4976.     {
  4977.       compiler_error ("could not overload `operator->()(...)'");
  4978.       return error_mark_node;
  4979.     }
  4980.       else if (result == error_mark_node)
  4981.     return error_mark_node;
  4982.  
  4983. #if 0
  4984.       /* Do this if we want the result of operator->() to inherit
  4985.      the type of the function it is subbing for.  */
  4986.       TREE_TYPE (result) = value_type;
  4987. #endif
  4988.  
  4989. #ifdef ESKIT
  4990.       {
  4991.     int used, size;
  4992.  
  4993.     /* Count the number of bytes of arguements to operator->(),
  4994.        not to the method itself.  In the tally, don't count bytes
  4995.        for pointer to member function or for the bytecount.  */
  4996.     parms = TREE_OPERAND (result, 1);
  4997.     bytecount = get_arglist_len_in_bytes (TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (parms))));
  4998.     used = size = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_VALUE (parms))));
  4999. #ifdef PUSH_ROUNDING
  5000.     size = PUSH_ROUNDING (size);
  5001. #endif
  5002.     used = (((size + PARM_BOUNDARY / BITS_PER_UNIT - 1)
  5003.          / (PARM_BOUNDARY / BITS_PER_UNIT))
  5004.         * (PARM_BOUNDARY / BITS_PER_UNIT));
  5005.     bytecount += used;
  5006.     TREE_CHAIN (TREE_CHAIN (parms))
  5007.       = tree_cons (NULL_TREE, build_int_2 (bytecount, 0),
  5008.                TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (parms))));
  5009.       }
  5010. #endif
  5011.  
  5012.       return result;
  5013.     }
  5014.  
  5015.   /* If we are referencing a virtual function from an object
  5016.      of effectively static type, then there is no need
  5017.      to go through the virtual function table.  */
  5018.   if (need_vtbl)
  5019.     {
  5020.       int use_vtable = 0;
  5021.       int fixed_type = resolves_to_fixed_type_p (instance);
  5022.  
  5023.       if (all_virtual == 1
  5024.       && DECL_VINDEX (function)
  5025.       && may_be_remote (basetype))
  5026.     use_vtable = 1;
  5027.       else if (DECL_VIRTUAL_P (function) && ! fixed_type)
  5028.     use_vtable = 1;
  5029.  
  5030.       if (fixed_type && DECL_ABSTRACT_VIRTUAL_P (function))
  5031.     {
  5032.       error_with_decl (function, "invalid call to abstract function `%s'");
  5033.       return error_mark_node;
  5034.     }
  5035.       if (use_vtable)
  5036.     {
  5037.       function = build_vfn_ref (&TREE_VALUE (parms), instance, DECL_VINDEX (function));
  5038.       TREE_TYPE (function) = build_pointer_type (fntype);
  5039.     }
  5040.     }
  5041. #ifdef SOS
  5042.   else if (basetype && TYPE_DYNAMIC (basetype))
  5043.     {
  5044.       function = build_array_ref (dtbl, DECL_DINDEX (function));
  5045.       TREE_TYPE (function) = build_pointer_type (fntype);
  5046.     }
  5047. #endif
  5048.  
  5049.   if (TREE_INLINE (function) && TREE_CODE (function) == FUNCTION_DECL)
  5050.     function = build1 (ADDR_EXPR, build_pointer_type (fntype), function);
  5051.   else function = default_conversion (function);
  5052.  
  5053.   result =
  5054.     build_nt (CALL_EXPR, function, parms, NULL_TREE);
  5055.  
  5056.   TREE_TYPE (result) = value_type;
  5057.   TREE_VOLATILE (result) = 1;
  5058.   TREE_RAISES (result)
  5059.     = TYPE_RAISES_EXCEPTIONS (fntype) || (parms && TREE_RAISES (parms));
  5060.   return result;
  5061. }
  5062.  
  5063. /* Similar to `build_method_call', but for overloaded non-member functions.
  5064.    The name of this function comes through NAME.  The name depends
  5065.    on PARMS.
  5066.  
  5067.    Note that this function must handle simple `C' promotions,
  5068.    as well as variable numbers of arguments (...), and
  5069.    default arguments to boot.
  5070.  
  5071.    If the overloading is successful, we return a treenode which
  5072.    contains the call to the function.
  5073.  
  5074.    If overloading produces candidates which are probabe, but not definite,
  5075.    we hold these candidates.  If FINAL_CP is non-zero, then we are free
  5076.    to assume that final_cp points to enough storage for all candidates that
  5077.    this function might generate.  The `harshness' array is preallocated for
  5078.    the first candidate, but not for subsequent ones.
  5079.  
  5080.    Note that the DECL_RTL of FUNCTION must be made to agree with this
  5081.    function's new name.  */
  5082.  
  5083. tree
  5084. build_overload_call (fnname, parms, complain, final_cp)
  5085.      tree fnname, parms;
  5086.      int complain;
  5087.      struct candidate *final_cp;
  5088. {
  5089.   /* must check for overloading here */
  5090.   tree overload_name, functions, function, parm;
  5091.   tree parmtypes = NULL_TREE, last = NULL_TREE;
  5092.   register tree outer;
  5093.   int length;
  5094.   int parmlength = list_length (parms);
  5095.  
  5096.   struct candidate *candidates, *cp;
  5097.   int rank_for_overload ();
  5098.  
  5099.   if (final_cp)
  5100.     {
  5101.       final_cp[0].evil = 0;
  5102.       final_cp[0].user = 0;
  5103.       final_cp[0].b_or_d = 0;
  5104.       final_cp[0].easy = 0;
  5105.       final_cp[0].function = 0;
  5106.       /* end marker.  */
  5107.       final_cp[1].evil = 1;
  5108.     }
  5109.  
  5110.   for (parm = parms; parm; parm = TREE_CHAIN (parm))
  5111.     {
  5112.       register tree t = TREE_TYPE (TREE_VALUE (parm));
  5113.  
  5114.       if (t == error_mark_node)
  5115.     return error_mark_node;
  5116.       if (TREE_CODE (t) == ARRAY_TYPE || TREE_CODE (t) == OFFSET_TYPE)
  5117.     {
  5118.       /* Perform the conversion from ARRAY_TYPE to POINTER_TYPE in place.
  5119.          Also convert OFFSET_TYPE entities to their normal selves.
  5120.          This eliminates needless calls to `compute_conversion_costs'.  */
  5121.       TREE_VALUE (parm) = default_conversion (TREE_VALUE (parm));
  5122.       t = TREE_TYPE (TREE_VALUE (parm));
  5123.     }
  5124.       last = build_tree_list (NULL_TREE, t);
  5125.       parmtypes = chainon (parmtypes, last);
  5126.     }
  5127.   if (last)
  5128.     TREE_CHAIN (last) = void_list_node;
  5129.   else
  5130.     parmtypes = void_list_node;
  5131.   overload_name = build_decl_overload (IDENTIFIER_POINTER (fnname), parmtypes, 0);
  5132.  
  5133.   /* Now check to see whether or not we can win.
  5134.      Note that if we are called from `build_method_call',
  5135.      then we cannot have a mis-match, because we would have
  5136.      already found such a winning case.  */
  5137.  
  5138.   if (IDENTIFIER_GLOBAL_VALUE (overload_name))
  5139.     if (TREE_CODE (IDENTIFIER_GLOBAL_VALUE (overload_name)) != TREE_LIST)
  5140.       return build_function_call (DECL_MAIN_VARIANT (IDENTIFIER_GLOBAL_VALUE (overload_name)), parms);
  5141.  
  5142.   functions = IDENTIFIER_GLOBAL_VALUE (fnname);
  5143.  
  5144.   if (functions == NULL_TREE)
  5145.     {
  5146.       if (complain)
  5147.     error ("only member functions apply");
  5148.       if (final_cp)
  5149.     final_cp->evil = 1;
  5150.       return error_mark_node;
  5151.     }
  5152.  
  5153.   if (TREE_CODE (functions) == FUNCTION_DECL)
  5154.     {
  5155.       functions = DECL_MAIN_VARIANT (functions);
  5156.       if (final_cp)
  5157.     {
  5158.       /* We are just curious whether this is a viable alternative or not.  */
  5159.       compute_conversion_costs (functions, parms, final_cp, parmlength);
  5160.       return functions;
  5161.     }
  5162.       else
  5163.     return build_function_call (functions, parms);
  5164.     }
  5165.  
  5166.   if (TREE_VALUE (functions) == NULL_TREE)
  5167.     {
  5168.       if (complain)
  5169.     error ("function `%s' declared overloaded, but no instances of that function declared",
  5170.            IDENTIFIER_POINTER (TREE_PURPOSE (functions)));
  5171.       return error_mark_node;
  5172.     }
  5173.  
  5174.   if (TREE_CODE (TREE_VALUE (functions)) == TREE_LIST)
  5175.     {
  5176.       register tree outer;
  5177.       length = 0;
  5178.  
  5179.       /* The list-of-lists should only occur for class things.  */
  5180.       assert (functions == IDENTIFIER_CLASS_VALUE (fnname));
  5181.  
  5182.       for (outer = functions; outer; outer = TREE_CHAIN (outer))
  5183.     {
  5184.       /* member functions.  */
  5185.       length += list_length (TREE_VALUE (TREE_VALUE (outer)));
  5186.       /* friend functions.  */
  5187.       length += list_length (TREE_TYPE (TREE_VALUE (outer)));
  5188.     }
  5189.     }
  5190.   else
  5191.     {
  5192.       length = list_length (functions);
  5193.     }
  5194.  
  5195.   if (final_cp)
  5196.     candidates = final_cp;
  5197.   else
  5198.     candidates = (struct candidate *)alloca ((length+1) * sizeof (struct candidate));
  5199.  
  5200.   cp = candidates;
  5201.  
  5202.   assert (TREE_CODE (TREE_VALUE (functions)) != TREE_LIST);
  5203.   /* OUTER is the list of FUNCTION_DECLS, in a TREE_LIST.  */
  5204.  
  5205.   for (outer = functions; outer; outer = TREE_CHAIN (outer))
  5206.     {
  5207.       function = TREE_VALUE (outer);
  5208.       if (TREE_CODE (function) != FUNCTION_DECL)
  5209.     {
  5210.       if (TREE_CODE (function) == CONST_DECL)
  5211.         error_with_decl (function, "enumeral value `%s' conflicts with function of same name");
  5212.       else if (TREE_CODE (function) == VAR_DECL)
  5213.         if (TREE_STATIC (function))
  5214.           error_with_decl (function, "variable `%s' conflicts with function of same name");
  5215.         else
  5216.           error_with_decl (function, "constant field `%s' conflicts with function of same name");
  5217.       else if (TREE_CODE (function) == TYPE_DECL)
  5218.         continue;
  5219.       else abort ();
  5220.       error ("at this point in file");
  5221.       continue;
  5222.     }
  5223.       function = DECL_MAIN_VARIANT (function);
  5224.       /* Can't use alloca here, since result might be
  5225.      passed to calling function.  */
  5226.       cp->harshness
  5227.     = (unsigned short *)oballoc ((parmlength+1) * sizeof (short));
  5228.       compute_conversion_costs (function, parms, cp, parmlength);
  5229.       if (cp[0].evil == 0)
  5230.     {
  5231.       cp[1].evil = 1;
  5232.       if (final_cp
  5233.           && cp[0].user == 0 && cp[0].b_or_d == 0
  5234.           && cp[0].easy <= 1)
  5235.         {
  5236.           final_cp[0].easy = cp[0].easy;
  5237.           return function;
  5238.         }
  5239.       cp++;
  5240.     }
  5241.     }
  5242.  
  5243.   if (cp - candidates)
  5244.     {
  5245.       tree rval = error_mark_node;
  5246.  
  5247.       /* Leave marker.  */
  5248.       cp[0].evil = 1;
  5249.       if (cp - candidates > 1)
  5250.     {
  5251.       struct candidate *best_cp
  5252.         = ideal_candidate (NULL_TREE, candidates,
  5253.                    cp - candidates, parms, parmlength);
  5254.       if (best_cp == 0)
  5255.         {
  5256.           if (complain)
  5257.         error ("call of overloaded `%s' is ambiguous", IDENTIFIER_POINTER (fnname));
  5258.           return error_mark_node;
  5259.         }
  5260.       else
  5261.         rval = best_cp->function;
  5262.     }
  5263.       else
  5264.     {
  5265.       cp -= 1;
  5266.       if (cp->evil > 1)
  5267.         {
  5268.           if (complain)
  5269.         error ("type conversion ambiguous");
  5270.         }
  5271.       else
  5272.         rval = cp->function;
  5273.     }
  5274.  
  5275.       if (final_cp)
  5276.     return rval;
  5277.  
  5278.       return build_function_call (rval, parms);
  5279.     }
  5280.   else if (complain)
  5281.     {
  5282.       tree name;
  5283.       char *err_name;
  5284.       /* Initialize name for error reporting.  */
  5285.       if (TREE_CODE (functions) == TREE_LIST)
  5286.     name = TREE_PURPOSE (functions);
  5287.       else
  5288.     name = DECL_ORIGINAL_NAME (functions);
  5289.  
  5290.       if (OPERATOR_NAME_P (name))
  5291.     {
  5292.       char *opname = operator_name_string (name);
  5293.       err_name = (char *)alloca (strlen (opname) + 12);
  5294.       sprintf (err_name, "operator %s", opname);
  5295.     }
  5296.       else
  5297.     err_name = IDENTIFIER_POINTER (name);
  5298.  
  5299.       report_type_mismatch (cp, parms, "function", err_name);
  5300.     }
  5301.   return error_mark_node;
  5302. }
  5303.  
  5304. void
  5305. init_class_processing ()
  5306. {
  5307.   current_class_stacksize = 10;
  5308.   current_class_base = (tree *)xmalloc(current_class_stacksize * sizeof (tree));
  5309.   current_class_stack = current_class_base;
  5310.  
  5311.   current_lang_stacksize = 10;
  5312.   current_lang_base = (tree *)xmalloc(current_lang_stacksize * sizeof (tree));
  5313.   current_lang_stack = current_lang_base;
  5314.  
  5315.   delta_name = get_identifier (VTABLE_DELTA_NAME);
  5316.   pfn_name = get_identifier (VTABLE_PFN_NAME);
  5317.  
  5318.   /* Keep these values lying around.  */
  5319.   minus_one_node = build_int_2 (-1, 0);
  5320.   the_null_vtable_entry = build_vtable_entry (integer_zero_node, integer_zero_node);
  5321.   base_layout_decl = build_lang_field_decl (FIELD_DECL, NULL_TREE, error_mark_node);
  5322.   TREE_TYPE (base_layout_decl) = make_node (RECORD_TYPE);
  5323.  
  5324.   obstack_init (&class_obstack);
  5325. }
  5326.  
  5327. /* Set current scope to NAME. CODE tells us if this is a
  5328.    STRUCT, UNION, or ENUM environment.
  5329.  
  5330.    NAME may end up being NULL_TREE if this is an anonymous or
  5331.    late-bound struct (as in "struct { ... } foo;")  */
  5332.  
  5333. /* Here's a subroutine we need because C lacks lambdas.  */
  5334. void
  5335. unuse_fields (type)
  5336.      tree type;
  5337. {
  5338.   tree fields;
  5339.  
  5340.   for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
  5341.     {
  5342.       if (TREE_CODE (fields) != FIELD_DECL)
  5343.     continue;
  5344.  
  5345.       TREE_USED (fields) = 0;
  5346.       if (DECL_ANON_UNION_ELEM (fields))
  5347.     unuse_fields (TREE_TYPE (fields));
  5348.     }
  5349. }
  5350.  
  5351. /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE to
  5352.    appropriate values, found by looking up the type definition of
  5353.    NAME (as a CODE).
  5354.  
  5355.    If MODIFY is 1, we set IDENTIFIER_CLASS_VALUE's of names
  5356.    which can be seen locally to the class. They are shadowed by
  5357.    any subsequent local declaration (including parameter names).
  5358.  
  5359.    If MODIFY is 2, we set IDENTIFIER_CLASS_VALUE's of names
  5360.    which have static meaning (i.e., static members, static
  5361.    member functions, enum declarations, etc).
  5362.  
  5363.    So that we may avoid calls to lookup_name, we cache the TYPE_DECL
  5364.    in the TREE_TYPE field of the name.
  5365.  
  5366.    For multiple inheritance, we perform a two-pass depth-first search
  5367.    of the type lattice.  The first pass performs a pre-order search,
  5368.    marking types after the type has had its fields installed in
  5369.    the appropriate IDENTIFIER_CLASS_VALUE slot.  The second pass merely
  5370.    unmarks the marked types.  If a field or member function name
  5371.    appears in an ambiguous way, the IDENTIFIER_CLASS_VALUE of
  5372.    that name becomes `error_mark_node'.  */
  5373.  
  5374. void
  5375. pushclass (type, modify)
  5376.      tree type;
  5377.      int modify;
  5378. {
  5379.   push_memoized_context (type, modify);
  5380.  
  5381.   *current_class_stack++ = current_class_name;
  5382.   *current_class_stack++ = current_class_type;
  5383.   if (current_class_stack >= current_class_base + current_class_stacksize - 1)
  5384.     {
  5385.       current_class_base =
  5386.     (tree *)xrealloc (current_class_base, current_class_stacksize + 10);
  5387.       current_class_stack = current_class_base + current_class_stacksize;
  5388.       current_class_stacksize += 10;
  5389.     }
  5390.  
  5391.   type = TYPE_MAIN_VARIANT (type);
  5392.   current_class_name = TYPE_NAME (type);
  5393.   if (TREE_CODE (current_class_name) == TYPE_DECL)
  5394.     current_class_name = DECL_NAME (current_class_name);
  5395.   current_class_type = type;
  5396.  
  5397.   if (type != prev_class_type && prev_class_type != NULL_TREE
  5398.       && current_class_stack == current_class_base + 2)
  5399.     {
  5400.       popclass (-1);
  5401.       prev_class_type = 0;
  5402.     }
  5403.  
  5404.   if (modify)
  5405.     {
  5406.       tree tags;
  5407.  
  5408.       if (type != prev_class_type)
  5409.     {
  5410.       build_mi_matrix (type);
  5411.       push_class_decls (type);
  5412.       free_mi_matrix ();
  5413.       prev_class_type = type;
  5414.     }
  5415.       else
  5416.     unuse_fields (type);
  5417.  
  5418.       tags = CLASSTYPE_TAGS (type);
  5419.       while (tags)
  5420.     {
  5421.       TREE_NONLOCAL (TREE_VALUE (tags)) = 1;
  5422.       pushtag (TREE_PURPOSE (tags), TREE_VALUE (tags));
  5423.       if (IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (tags)) == NULL_TREE
  5424.           && TREE_CODE (TYPE_NAME (TREE_VALUE (tags))) == TYPE_DECL)
  5425.         IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (tags))
  5426.           = TYPE_NAME (TREE_VALUE (tags));
  5427.       tags = TREE_CHAIN (tags);
  5428.     }
  5429.     }
  5430.   else
  5431.     pushlevel_class ();
  5432. }
  5433.  
  5434. /* Get out of the current class scope. If we were in a class scope
  5435.    previously, that is the one popped to.  The flag MODIFY tells
  5436.    whether the current scope declarations needs to be modified
  5437.    as a result of popping to the new scope.  */
  5438. void
  5439. popclass (modify)
  5440.      int modify;
  5441. {
  5442.   if (modify < 0)
  5443.     {
  5444.       /* Back this old class out completely.  */
  5445.       tree tags = CLASSTYPE_TAGS (prev_class_type);
  5446.  
  5447.       pop_class_decls (prev_class_type);
  5448.       while (tags)
  5449.     {
  5450.       TREE_NONLOCAL (TREE_VALUE (tags)) = 0;
  5451.       IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (tags)) = NULL_TREE;
  5452.       tags = TREE_CHAIN (tags);
  5453.     }
  5454.       return;
  5455.     }
  5456.   if (modify)
  5457.     {
  5458.       /* Just remove from this class what didn't make
  5459.      it into IDENTIFIER_CLASS_VALUE.  */
  5460.       tree tags = CLASSTYPE_TAGS (current_class_type);
  5461.  
  5462.       while (tags)
  5463.     {
  5464.       TREE_NONLOCAL (TREE_VALUE (tags)) = 0;
  5465.       IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (tags)) = NULL_TREE;
  5466.       tags = TREE_CHAIN (tags);
  5467.     }
  5468.     }
  5469.   else
  5470.     poplevel_class ();
  5471.  
  5472.   current_class_type = *--current_class_stack;
  5473.   current_class_name = *--current_class_stack;
  5474.  
  5475.   if (current_class_type)
  5476.     {
  5477.       if (CLASSTYPE_VTBL_PTR (current_class_type))
  5478.     {
  5479.       current_vtable_decl = lookup_name (DECL_NAME (CLASSTYPE_VTBL_PTR (current_class_type)));
  5480.       if (current_vtable_decl)
  5481.         current_vtable_decl = build_indirect_ref (current_vtable_decl, 0);
  5482.     }
  5483.       current_class_decl = lookup_name (get_identifier (THIS_NAME));
  5484.       if (current_class_decl)
  5485.     {
  5486.       if (TREE_CODE (TREE_TYPE (current_class_decl)) == POINTER_TYPE)
  5487.         {
  5488.           /* Can't call build_indirect_ref here, because it has special
  5489.          logic to return C_C_D given this argument.  */
  5490.           C_C_D = build1 (INDIRECT_REF, current_class_type, current_class_decl);
  5491.           TREE_READONLY (C_C_D) = TREE_READONLY (TREE_TYPE (TREE_TYPE (current_class_decl)));
  5492.           TREE_VOLATILE (C_C_D) = TREE_VOLATILE (TREE_TYPE (TREE_TYPE (current_class_decl)));
  5493.         }
  5494.       else
  5495.         C_C_D = current_class_decl;
  5496.     }
  5497.       else C_C_D = NULL_TREE;
  5498.     }
  5499.   else
  5500.     {
  5501.       current_class_decl = NULL_TREE;
  5502.       current_vtable_decl = NULL_TREE;
  5503.       C_C_D = NULL_TREE;
  5504.     }
  5505.  
  5506.   pop_memoized_context (modify);
  5507. }
  5508.  
  5509. /* Set global variables CURRENT_LANG_NAME to appropriate value
  5510.    so that behavior of name-mangline machinery is correct.  */
  5511.  
  5512. void
  5513. push_lang_context (name)
  5514.      tree name;
  5515. {
  5516.   *current_lang_stack++ = current_lang_name;
  5517.   if (current_lang_stack >= current_lang_base + current_lang_stacksize)
  5518.     {
  5519.       current_lang_base =
  5520.     (tree *)xrealloc (current_lang_base, current_lang_stacksize + 10);
  5521.       current_lang_stack = current_lang_base + current_lang_stacksize;
  5522.       current_lang_stacksize += 10;
  5523.     }
  5524.  
  5525.   if (name == lang_name_cplusplus)
  5526.     {
  5527.       strict_prototype = strict_prototypes_lang_cplusplus;
  5528.       current_lang_name = name;
  5529.     }
  5530.   else if (name == lang_name_c)
  5531.     {
  5532.       strict_prototype = strict_prototypes_lang_c;
  5533.       current_lang_name = name;
  5534.     }
  5535.   else
  5536.     error ("language string `\"%s\"' not recognized", IDENTIFIER_POINTER (name));
  5537. }
  5538.   
  5539. /* Get out of the current language scope.  */
  5540. void
  5541. pop_lang_context ()
  5542. {
  5543.   current_lang_name = *--current_lang_stack;
  5544.   if (current_lang_name == lang_name_cplusplus)
  5545.     strict_prototype = strict_prototypes_lang_cplusplus;
  5546.   else if (current_lang_name == lang_name_c)
  5547.     strict_prototype = strict_prototypes_lang_c;
  5548. }
  5549.  
  5550. int
  5551. root_lang_context_p ()
  5552. {
  5553.   return current_lang_stack == current_lang_base;
  5554. }
  5555.  
  5556. /* Type instantiation routines.  */
  5557.  
  5558. /* This function will instantiate the type of the expression given
  5559.    in RHS to match the type of LHSTYPE.  If LHSTYPE is NULL_TREE,
  5560.    or other errors exist, the TREE_TYPE of RHS will be ERROR_MARK_NODE.
  5561.  
  5562.    This function is used in build_modify_expr, actualparameterlist,
  5563.    build_c_cast, and compute_conversion_costs.  */
  5564. tree
  5565. instantiate_type (lhstype, rhs, complain)
  5566.      tree lhstype, rhs;
  5567.      int complain;
  5568. {
  5569.   if (TREE_CODE (rhs) == OP_IDENTIFIER)
  5570.     return build_instantiated_decl (lhstype, rhs);
  5571.  
  5572.   if (TREE_CODE (lhstype) == UNKNOWN_TYPE)
  5573.     {
  5574.       if (complain)
  5575.     error ("not enough type information");
  5576.       return error_mark_node;
  5577.     }
  5578.  
  5579.   if (TREE_TYPE (rhs) != NULL_TREE
  5580.       && ! (type_unknown_p (rhs)))
  5581.     return rhs;
  5582.  
  5583.   /* This should really only be used when attempting to distinguish
  5584.      what sort of a pointer to function we have.  For now, any
  5585.      arithmethic operation which is not supported on pointers
  5586.      is rejected as an error.  */
  5587.  
  5588.   switch (TREE_CODE (rhs))
  5589.     {
  5590.     case TYPE_EXPR:
  5591.     case CONVERT_EXPR:
  5592.     case SAVE_EXPR:
  5593.     case CONSTRUCTOR:
  5594.     case BUFFER_REF:
  5595.       assert (0);
  5596.       return error_mark_node;
  5597.  
  5598.     case INDIRECT_REF:
  5599.     case ARRAY_REF:
  5600.       TREE_TYPE (rhs) = lhstype;
  5601.       lhstype = build_pointer_type (lhstype);
  5602.       TREE_OPERAND (rhs, 0)
  5603.     = instantiate_type (lhstype, TREE_OPERAND (rhs, 0), complain);
  5604.       if (TREE_OPERAND (rhs, 0) == error_mark_node)
  5605.     return error_mark_node;
  5606.  
  5607.       return rhs;
  5608.  
  5609.     case NOP_EXPR:
  5610.       rhs = copy_node (TREE_OPERAND (rhs, 0));
  5611.       TREE_TYPE (rhs) = unknown_type_node;
  5612.       return instantiate_type (lhstype, rhs, complain);
  5613.  
  5614.     case COMPONENT_REF:
  5615.       {
  5616.     tree field = TREE_OPERAND (rhs, 1);
  5617.     if (TREE_CODE (field) == TREE_LIST)
  5618.       {
  5619.         tree function = instantiate_type (lhstype, field, complain);
  5620.         if (function == error_mark_node)
  5621.           return error_mark_node;
  5622.         assert (TREE_CODE (function) == FUNCTION_DECL);
  5623.         if (DECL_VIRTUAL_P (function))
  5624.           {
  5625.         tree base = TREE_OPERAND (rhs, 0);
  5626.         tree base_ptr = build_unary_op (ADDR_EXPR, base, 0);
  5627.         if (base_ptr == error_mark_node)
  5628.           return error_mark_node;
  5629.         base_ptr = convert_pointer_to (DECL_VCONTEXT (function), base_ptr);
  5630.         if (base_ptr == error_mark_node)
  5631.           return error_mark_node;
  5632.         return build_vfn_ref (&base_ptr, base, DECL_VINDEX (function));
  5633.           }
  5634.         return function;
  5635.       }
  5636.  
  5637.     assert (TREE_CODE (field) == FIELD_DECL);
  5638.     assert (!(TREE_CODE (TREE_TYPE (field)) == FUNCTION_TYPE
  5639.           || TREE_CODE (TREE_TYPE (field)) == METHOD_TYPE));
  5640.  
  5641.     TREE_TYPE (rhs) = lhstype;
  5642.     /* First look for an exact match  */
  5643.  
  5644.     while (field && TREE_TYPE (field) != lhstype)
  5645.       field = TREE_CHAIN (field);
  5646.     if (field)
  5647.       {
  5648.         TREE_OPERAND (rhs, 1) = field;
  5649.         return rhs;
  5650.       }
  5651.  
  5652.     /* No exact match found, look for a compatible function.  */
  5653.     field = TREE_OPERAND (rhs, 1);
  5654.     while (field && ! comptypes (lhstype, TREE_TYPE (field), 0))
  5655.       field = TREE_CHAIN (field);
  5656.     if (field)
  5657.       {
  5658.         TREE_OPERAND (rhs, 1) = field;
  5659.         field = TREE_CHAIN (field);
  5660.         while (field && ! comptypes (lhstype, TREE_TYPE (field), 0))
  5661.           field = TREE_CHAIN (field);
  5662.         if (field)
  5663.           {
  5664.         if (complain)
  5665.           error ("ambiguous overload for COMPONENT_REF requested");
  5666.         return error_mark_node;
  5667.           }
  5668.       }
  5669.     else
  5670.       {
  5671.         if (complain)
  5672.           error ("no appropriate overload exists for COMPONENT_REF");
  5673.         return error_mark_node;
  5674.       }
  5675.     return rhs;
  5676.       }
  5677.  
  5678.     case TREE_LIST:
  5679.       {
  5680.     tree elem, baselink, name;
  5681.     int globals = overloaded_globals_p (rhs);
  5682.  
  5683.     /* First look for an exact match.  Search either overloaded
  5684.        functions or member functions.  May have to undo what
  5685.        `default_conversion' or `datatype' might do to lhstype.  */
  5686.  
  5687.     if (TREE_CODE (lhstype) == POINTER_TYPE)
  5688.       if (TREE_CODE (TREE_TYPE (lhstype)) == FUNCTION_TYPE
  5689.           || TREE_CODE (TREE_TYPE (lhstype)) == METHOD_TYPE)
  5690.         lhstype = TREE_TYPE (lhstype);
  5691.       else
  5692.         {
  5693.           if (complain)
  5694.         error ("invalid type combination for overload");
  5695.           return error_mark_node;
  5696.         }
  5697.  
  5698.     if (TREE_CODE (lhstype) != FUNCTION_TYPE && globals > 0)
  5699.       {
  5700.         if (complain)
  5701.           error ("cannot resolve overloaded function `%s' based on non-function type",
  5702.              IDENTIFIER_POINTER (TREE_PURPOSE (rhs)));
  5703.         return error_mark_node;
  5704.       }
  5705.  
  5706.     if (globals > 0)
  5707.       {
  5708.         assert (TREE_CODE (TREE_VALUE (rhs)) == FUNCTION_DECL);
  5709.         elem = rhs;
  5710.         while (elem)
  5711.           if (TREE_TYPE (TREE_VALUE (elem)) != lhstype)
  5712.         elem = TREE_CHAIN (elem);
  5713.           else
  5714.         return TREE_VALUE (elem);
  5715.         /* No exact match found, look for a compatible function.  */
  5716.         elem = rhs;
  5717.         while (elem && ! comp_target_types (lhstype, TREE_TYPE (TREE_VALUE (elem)), 1))
  5718.           elem = TREE_CHAIN (elem);
  5719.         if (elem)
  5720.           {
  5721.         tree save_elem = TREE_VALUE (elem);
  5722.         elem = TREE_CHAIN (elem);
  5723.         while (elem && ! comp_target_types (lhstype, TREE_TYPE (TREE_VALUE (elem)), 0))
  5724.           elem = TREE_CHAIN (elem);
  5725.         if (elem)
  5726.           {
  5727.             if (complain)
  5728.               error ("ambiguous overload for overloaded function requested");
  5729.             return error_mark_node;
  5730.           }
  5731.         return save_elem;
  5732.           }
  5733.         if (complain)
  5734.           {
  5735.         if (TREE_CHAIN (rhs))
  5736.           error ("no appropriate overload for overloaded function `%s' exists",
  5737.              IDENTIFIER_POINTER (TREE_PURPOSE (rhs)));
  5738.         else
  5739.           error ("function `%s' has inappropriate type signature",
  5740.              IDENTIFIER_POINTER (TREE_PURPOSE (rhs)));
  5741.           }
  5742.         return error_mark_node;
  5743.       }
  5744.  
  5745.     if (TREE_NONLOCAL (rhs))
  5746.       {
  5747.         /* Got to get it as a baselink.  */
  5748.         rhs = lookup_fnfields (CLASSTYPE_AS_LIST (current_class_type),
  5749.                    TREE_PURPOSE (rhs), 0);
  5750.       }
  5751.     else
  5752.       {
  5753.         assert (TREE_CHAIN (rhs) == NULL_TREE);
  5754.         if (TREE_CODE (TREE_VALUE (rhs)) == TREE_LIST)
  5755.           rhs = TREE_VALUE (rhs);
  5756.         assert (TREE_CODE (TREE_VALUE (rhs)) == FUNCTION_DECL);
  5757.       }
  5758.  
  5759.     for (baselink = rhs; baselink;
  5760.          baselink = next_baselink (baselink))
  5761.       {
  5762.         elem = TREE_VALUE (baselink);
  5763.         while (elem)
  5764.           if (TREE_TYPE (elem) != lhstype)
  5765.         elem = TREE_CHAIN (elem);
  5766.           else
  5767.         return elem;
  5768.       }
  5769.  
  5770.     /* No exact match found, look for a compatible method.  */
  5771.     for (baselink = rhs; baselink;
  5772.          baselink = next_baselink (baselink))
  5773.       {
  5774.         elem = TREE_VALUE (baselink);
  5775.         while (elem && ! comp_target_types (lhstype, TREE_TYPE (elem), 1))
  5776.           elem = TREE_CHAIN (elem);
  5777.         if (elem)
  5778.           {
  5779.         tree save_elem = elem;
  5780.         elem = TREE_CHAIN (elem);
  5781.         while (elem && ! comp_target_types (lhstype, TREE_TYPE (elem), 0))
  5782.           elem = TREE_CHAIN (elem);
  5783.         if (elem)
  5784.           {
  5785.             if (complain)
  5786.               error ("ambiguous overload for overloaded method requested");
  5787.             return error_mark_node;
  5788.           }
  5789.         return save_elem;
  5790.           }
  5791.         name = DECL_ORIGINAL_NAME (TREE_VALUE (rhs));
  5792.         if (TREE_CODE (lhstype) == FUNCTION_TYPE && globals < 0)
  5793.           {
  5794.         /* Try to instantiate from non-member functions.  */
  5795.         rhs = IDENTIFIER_GLOBAL_VALUE (name);
  5796.         if (rhs && TREE_CODE (rhs) == TREE_LIST)
  5797.           {
  5798.             /* This code seems to be missing a `return'.  */
  5799.             abort ();
  5800.             instantiate_type (lhstype, rhs, complain);
  5801.           }
  5802.           }
  5803.  
  5804.         if (complain)
  5805.           error ("no static member functions named `%s'",
  5806.              IDENTIFIER_POINTER (name));
  5807.         return error_mark_node;
  5808.       }
  5809.       }
  5810.  
  5811.     case CALL_EXPR:
  5812.       /* This is too hard for now.  */
  5813.       assert (0);
  5814.       return error_mark_node;
  5815.  
  5816.     case PLUS_EXPR:
  5817.     case MINUS_EXPR:
  5818.     case COMPOUND_EXPR:
  5819.       TREE_OPERAND (rhs, 0) = instantiate_type (lhstype, TREE_OPERAND (rhs, 0), complain);
  5820.       if (TREE_OPERAND (rhs, 0) == error_mark_node)
  5821.     return error_mark_node;
  5822.       TREE_OPERAND (rhs, 1) = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), complain);
  5823.       if (TREE_OPERAND (rhs, 1) == error_mark_node)
  5824.     return error_mark_node;
  5825.  
  5826.       TREE_TYPE (rhs) = lhstype;
  5827.       return rhs;
  5828.  
  5829.     case MULT_EXPR:
  5830.     case TRUNC_DIV_EXPR:
  5831.     case FLOOR_DIV_EXPR:
  5832.     case CEIL_DIV_EXPR:
  5833.     case ROUND_DIV_EXPR:
  5834.     case RDIV_EXPR:
  5835.     case TRUNC_MOD_EXPR:
  5836.     case FLOOR_MOD_EXPR:
  5837.     case CEIL_MOD_EXPR:
  5838.     case ROUND_MOD_EXPR:
  5839.     case FIX_ROUND_EXPR:
  5840.     case FIX_FLOOR_EXPR:
  5841.     case FIX_CEIL_EXPR:
  5842.     case FIX_TRUNC_EXPR:
  5843.     case FLOAT_EXPR:
  5844.     case NEGATE_EXPR:
  5845.     case ABS_EXPR:
  5846.     case MAX_EXPR:
  5847.     case MIN_EXPR:
  5848.     case FFS_EXPR:
  5849.  
  5850.     case BIT_AND_EXPR:
  5851.     case BIT_IOR_EXPR:
  5852.     case BIT_XOR_EXPR:
  5853.     case LSHIFT_EXPR:
  5854.     case RSHIFT_EXPR:
  5855.     case LROTATE_EXPR:
  5856.     case RROTATE_EXPR:
  5857.  
  5858.     case PREINCREMENT_EXPR:
  5859.     case PREDECREMENT_EXPR:
  5860.     case POSTINCREMENT_EXPR:
  5861.     case POSTDECREMENT_EXPR:
  5862.       if (complain)
  5863.     error ("illegal operation on uninstantiated type");
  5864.       return error_mark_node;
  5865.  
  5866.     case TRUTH_AND_EXPR:
  5867.     case TRUTH_OR_EXPR:
  5868.     case LT_EXPR:
  5869.     case LE_EXPR:
  5870.     case GT_EXPR:
  5871.     case GE_EXPR:
  5872.     case EQ_EXPR:
  5873.     case NE_EXPR:
  5874.     case TRUTH_ANDIF_EXPR:
  5875.     case TRUTH_ORIF_EXPR:
  5876.     case TRUTH_NOT_EXPR:
  5877.       if (complain)
  5878.     error ("not enough type information");
  5879.       return error_mark_node;
  5880.  
  5881.     case COND_EXPR:
  5882.       if (type_unknown_p (TREE_OPERAND (rhs, 0)))
  5883.     {
  5884.       if (complain)
  5885.         error ("not enough type information");
  5886.       return error_mark_node;
  5887.     }
  5888.       TREE_OPERAND (rhs, 1) = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), complain);
  5889.       if (TREE_OPERAND (rhs, 1) == error_mark_node)
  5890.     return error_mark_node;
  5891.       TREE_OPERAND (rhs, 2) = instantiate_type (lhstype, TREE_OPERAND (rhs, 2), complain);
  5892.       if (TREE_OPERAND (rhs, 2) == error_mark_node)
  5893.     return error_mark_node;
  5894.  
  5895.       TREE_TYPE (rhs) = lhstype;
  5896.       return rhs;
  5897.  
  5898.     case MODIFY_EXPR:
  5899.       TREE_OPERAND (rhs, 1) = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), complain);
  5900.       if (TREE_OPERAND (rhs, 1) == error_mark_node)
  5901.     return error_mark_node;
  5902.  
  5903.       TREE_TYPE (rhs) = lhstype;
  5904.       return rhs;
  5905.       
  5906.     case ADDR_EXPR:
  5907.       if (TREE_CODE (lhstype) != POINTER_TYPE)
  5908.     {
  5909.       if (complain)
  5910.         error ("type for resolving address of overloaded function must be pointer type");
  5911.       return error_mark_node;
  5912.     }
  5913.       TREE_TYPE (rhs) = lhstype;
  5914.       lhstype = TREE_TYPE (lhstype);
  5915.       TREE_OPERAND (rhs, 0) = instantiate_type (lhstype, TREE_OPERAND (rhs, 0), complain);
  5916.       if (TREE_OPERAND (rhs, 0) == error_mark_node)
  5917.     return error_mark_node;
  5918.  
  5919.       mark_addressable (TREE_OPERAND (rhs, 0));
  5920.       return rhs;
  5921.  
  5922.     case ENTRY_VALUE_EXPR:
  5923.       assert (0);
  5924.       return error_mark_node;
  5925.  
  5926.     case ERROR_MARK:
  5927.       return error_mark_node;
  5928.  
  5929.     default:
  5930.       assert (0);
  5931.       return error_mark_node;
  5932.     }
  5933. }
  5934.  
  5935. /* This routine is called when we finally know the type of expression
  5936.    we are looking for.  If the operator encoded by EXP can take an
  5937.    argument of type TYPE, return the FUNCTION_DECL for that operator.  */
  5938. tree
  5939. build_instantiated_decl (type, exp)
  5940.      tree type, exp;
  5941. {
  5942.   tree parmtypes, decl, name;
  5943.  
  5944.   assert (TREE_CODE (exp) == OP_IDENTIFIER);
  5945.   type = datatype (type);
  5946.   if (TREE_CODE (type) != POINTER_TYPE
  5947.       || (TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE
  5948.       && TREE_CODE (TREE_TYPE (type)) != METHOD_TYPE))
  5949.     {
  5950.       error ("invalid type used to resolve overloaded function");
  5951.       return error_mark_node;
  5952.     }
  5953.  
  5954.  
  5955.   /* Now we know the type of this function, so overload it.  */
  5956.   parmtypes = TYPE_ARG_TYPES (TREE_TYPE (type));
  5957.   name = build_operator_fnname (&TREE_VALUE (exp), parmtypes, 0);
  5958.   if (name)
  5959.     {
  5960.       name = build_decl_overload (IDENTIFIER_POINTER (name), parmtypes, 1);
  5961.       decl = lookup_name (name);
  5962.       if (decl)
  5963.     return decl;
  5964.       error ("no suitable declaration of `operator %s' for overloading",
  5965.          operator_name_string (name));
  5966.       return error_mark_node;
  5967.     }
  5968.   return error_mark_node;
  5969. }
  5970.  
  5971. /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
  5972.    for the given TYPE.  */
  5973. static tree
  5974. get_vtable_name (type)
  5975.      tree type;
  5976. {
  5977.   char *buf = (char *)alloca (sizeof (VTABLE_NAME_FORMAT)
  5978.                   + TYPE_NAME_LENGTH (type)
  5979.                   + 2);
  5980.   sprintf (buf, VTABLE_NAME_FORMAT, TYPE_NAME_STRING (type));
  5981.   return get_identifier (buf);
  5982. }
  5983.  
  5984. /* Return the name of the virtual function pointer field
  5985.    (as an IDENTIFIER_NODE) for the given TYPE.  Note that
  5986.    this may have to look back through base types to find the
  5987.    ultimate field name.  (For single inheritance, these could
  5988.    all be the same name.  Who knows for multiple inheritance).  */
  5989. static tree
  5990. get_vfield_name (type)
  5991.      tree type;
  5992. {
  5993.   char *buf;
  5994.  
  5995.   while (CLASSTYPE_N_BASECLASSES (type)
  5996.      && TYPE_VIRTUAL_P (CLASSTYPE_BASECLASS (type, 1))
  5997.      && ! CLASSTYPE_VIA_VIRTUAL (type, 1))
  5998.     type = CLASSTYPE_BASECLASS (type, 1);
  5999.  
  6000.   buf = (char *)alloca (sizeof (VFIELD_NAME_FORMAT)
  6001.             + TYPE_NAME_LENGTH (type)
  6002.             + 2);
  6003.   sprintf (buf, VFIELD_NAME_FORMAT, TYPE_NAME_STRING (type));
  6004.   return get_identifier (buf);
  6005. }
  6006.  
  6007. void
  6008. print_class_statistics ()
  6009. {
  6010. #ifdef GATHER_STATISTICS
  6011.   fprintf (stderr, "convert_harshness = %d\n", n_convert_harshness);
  6012.   fprintf (stderr, "compute_conversion_costs = %d\n", n_compute_conversion_costs);
  6013.   fprintf (stderr, "build_method_call = %d (inner = %d)\n",
  6014.        n_build_method_call, n_inner_fields_searched);
  6015.   if (n_vtables)
  6016.     {
  6017.       fprintf (stderr, "vtables = %d; vtable searches = %d\n",
  6018.            n_vtables, n_vtable_searches);
  6019.       fprintf (stderr, "vtable entries = %d; vtable elems = %d\n",
  6020.            n_vtable_entries, n_vtable_elems);
  6021.     }
  6022. #endif
  6023. }
  6024.